UMBC CMSC 331 Java Review of objects and variables in Java.

Slides:



Advertisements
Similar presentations
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Java Review Interface, Casting, Generics, Iterator.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Type Checking.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
1 Java Object Model Part 1. 2 Type Definition: set of values – a set of values and set of operations –a set of operations that can be applied to those.
CS 330 Programming Languages 10 / 24 / 2006 Instructor: Michael Eckmann.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
OOP Languages: Java vs C++
Laboratory Study October, The very first example, traditional "Hello World!" program: public class first { public static void main (String[ ]
Programming Languages and Paradigms Object-Oriented Programming.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
EECE 310: Software Engineering Lecture 2: Understanding Objects in Java and Types.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
Java Reflection. Compile-Time vs Run-Time Some data you know at compile time: int area = radius*radius*3.14; The “3.14” is set – known at compile time.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Programming Languages and Paradigms Object-Oriented Programming.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Introduction to Java Java Translation Program Structure
Object Oriented Software Development
Copyright © Curt Hill Inheritance and Polymorphism A Powerful Technique.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Using Objects. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L7: Objects Slide 2 Java.
CMSC 330: Organization of Programming Languages Java Generics.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Polymorphism CMPS Poly-morphism Means “many-forms” Means different things in biology, chemistry, computer science Means different things to functional.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 4 is due Nov. 20 (next Friday). After today you should know everything you need for assignment.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Memory Management in Java Mr. Gerb Computer Science 4.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Design issues for Object-Oriented Languages
EECE 309: Software Engineering
JAVA MULTIPLE CHOICE QUESTION.
Programming Language Concepts (CIS 635)
Java Review: Reference Types
Objects First with Java CITS1001
Computer Science II Exam 1 Review.
Java Programming Language
CMSC 202 Polymorphism.
Sampath Kumar S Assistant Professor, SECE
Core Concepts.
Polymorphism.
Java Programming Language
CMSC 202 Constructors Version 9/10.
String Objects & its Methods
Presentation transcript:

UMBC CMSC 331 Java Review of objects and variables in Java

UMBC CMSC 331 Java 2 variables & objects –what happens when you run this? String a = “foo”; System.out.println (a); –it prints foo –what is “foo”? –a string literal that evaluates to a String object –what is a? –a variable whose value is an object reference –what is String a = “foo”? –a declaration and an assignment in one “foo” (String) a

UMBC CMSC 331 Java 3 method calls –what happens when you run this? String a = “foo”; String b = a.toUpperCase (); System.out.println (b); –it prints foo –what is toUpperCase? a method of class String type is String -> String declared as public String toUpperCase () –what is a.toUpperCase ()? a method call on the object a –does it change a? no, it creates a new string “foo” (String) a “foo” (String) b

UMBC CMSC 331 Java 4 null references –what happens when you run this? String a = null; System.out.println (a); –it prints null –what happens when you run this? String a = null; String b = a.toUpperCase (); System.out.println (b); it throws a NullPointerException –why? because a method call must have a subject

UMBC CMSC 331 Java 5 sharing & equality –what happens when you run this? String a = “foo”; String b = “foo”; System.out.println (b); –it prints foo –is that the same as this? String a = “foo”; String b = a; System.out.println (b); –yes, because String is immutable. – There is no way to distinguish these cases and, in fact, Java virtual machine may produce upper or lower state in this case. “foo” (String) a b a b

UMBC CMSC 331 Java 6 mutable containers –what happens when you run this? Vector v = new Vector (); String a = “foo”; v.addElement (a); System.out.println (v.lastElement ()); –it prints foo –what happens when you run this? Vector v = new Vector (); String a = “foo”; String b = “foo”; v.addElement (a); System.out.println (v.lastElement ()); v.addElement (b); System.out.println (v.lastElement ()); –it prints foo (Vector) v “foo” (String) a b

UMBC CMSC 331 Java 7 aliasing –what about this? Vector v = new Vector (); Vector q = v; String a = “foo”; v.addElement (a); System.out.println (q.lastElement ()); –it prints foo –why? –because v and q are aliased: they are names for the same object –what if we now do this? if (v == q) System.out.println (“same object”); if (v.equals (q)) System.out.println (“same value”); –it prints same object same value (Vector) v “foo” (String) a q Aliasing occurs when several different identifiers refer to the same object. The term is very general and is used in many contexts.

UMBC CMSC 331 Java 8 aliasing & immutables –what does this do? String a = “foo”; String b = a; a.toUpperCase (); System.out.println (b); it prints foo –why? because strings are immutable –The objects created by the toUpperCase method is eventually GCed (garbage collected.) “foo” (String) a b a “FOO” (String) b

UMBC CMSC 331 Java 9 polymorphism what does this do? Vector v = new Vector (); Vector e = new Vector () v.addElement (e); e.addElement (“foo”); System.out.println ( ((Vector) v.lastElement ()).lastElement ()); it prints foo what kind of method is addElement? a polymorphic one type is Vector, Object -> void declared as public void addElement (Object o) (Vector) v e (String) “foo”

UMBC CMSC 331 Java 10 On polymorphism First identified by Christopher Strachey (1967) and developed by Hindley and Milner, allowing types such as a list of anything. E.g. in Haskell we can define a function which operates on a list of objects of any type a (a is a type variable). length :: [a] -> Int Polymorphic typing allows strong type checking as well as generic functions. ML in 1976 was the first language with polymorphic typing. Ad-hoc polymorphism (aka overloading) is the ability to use the same syntax for objects of different types, e.g. "+" for addition of reals and integers. In OOP, the term is used to describe variables which may refer at run-time to objects of different classes.

UMBC CMSC 331 Java 11 reference loops can i even add v to itself? Vector v = new Vector (); v.addElement (v); System.out.println (v.lastElement ()) yes, try it! and this? v.addElement (5); no, 5 is a primitive value, not an object (Vector) v “foo”

UMBC CMSC 331 Java 12 a pair of methods some types –what are the types of addElement, lastElement? addElement : Vector, Object -> void lastElement : Vector -> Object a puzzle –how are x and e related after this? v.addElement (e); x = v.lastElement (); –they denote the same object –can the compiler infer that? –no! not even that x and e have the same class

UMBC CMSC 331 Java 13 downcasts –what does this do? Vector v = new Vector (); String a = “foo”; v.addElement (a); String b = v.lastElement (); System.out.println (b); –compiler rejects it: v.lastElement doesn’t return a String! –what does this do? Vector v = new Vector (); String a = “foo”; v.addElement (a); String b = (String) v.lastElement (); System.out.println (b); –it prints foo

UMBC CMSC 331 Java 14 upcasting and downcasting Suppose we have object O of class C1 with superclass C2 In Java, upcasting is automatic but downcasting must be explicit. Upcasting: treating O as a C2 Downcasting: treating O as a C1

UMBC CMSC 331 Java 15 variable & object classes –what does this do? Vector v = new Vector (); String a = “foo”; v.addElement (a); Object o = v.lastElement (); System.out.println (o.getClass ()); –it prints java.lang.String –what’s going on here? getClass returns an object representing a class o.getClass () is the class o has at runtime System.out.println prints a string representation, ie, the name

UMBC CMSC 331 Java 16 Some key concepts variables & objects –variables hold object references (or primitive values like 5) –null is a special object reference sharing, equality & mutability –distinct objects can have the same value –state is held in value of instance variables –an object can be mutable (state may change) or immutable –two variables can point to the same object; changing one affects the other methods –a method has a ‘subject’ or ‘target’ object –may be polymorphic, ie. work on several types of object compile-time & runtime types –an object has a type at runtime: the class of its constructor –a variable has a declared, compile-time type or class –runtime class is subclass of compile-time class