cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf ·...

35
CS121/IS223 Week 12, Slide 1 CS121/IS223 Object Reference Variables Dr Olly Gotel [email protected] http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors

Transcript of cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf ·...

Page 1: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 1

CS121/IS223

Object Reference Variables

Dr Olly Gotel [email protected] http://csis.pace.edu/~ogotel

Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors

Page 2: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 2

Agenda

•  Make sure you have completed lab 1 and the shopping questions in lab 2

•  Objects & object reference variables - what are these object things anyway?

•  Understanding parameter passing – passing objects is not the same as passing primitives

Finish Lab 2 after this week

Page 3: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 3

Reminder…

class Car{ private String colour, licence; private double speed, fuel;

public void honk(){ System.out.println(“Beep Beep!”); } }

class CarTester{ public static void main(String[] args){ Car myCar = new Car(); myCar.honk(); } }

Instance variables – private data members - means can only be changed inside the class (by its methods)

Instance method – public means that it can be called from outside the class on objects of Car class

Primary class

Driver class with main() method

Make a new car object & call it myCar

Get myCar to honk its horn

Use visibility to enforce encapsulation

Page 4: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 4

Object Declaration, Creation, Assignment

Car myCar = new Car();

1 - Declare a reference variable , of class type Car, called myCar

2 - Create a new Car object

3 - Link the new object to the named reference variable

What is going on here? 3 distinct things… Need to step back a moment…

(Called an object reference variable)

(1)

(2) (3)

Page 5: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 5

What is Going On?

1.  Tells the JVM to allocate space for a reference variable on the stack; reference variable is called myCar; it is of type Car •  Stack – smallish area of memory; holds an address in

memory (where to find an object); we know how much memory we need to hold an address

2.  Tells the JVM to allocate space for a new Car object on the heap •  Heap - big area of memory; holds the object because we do

not know how big it will be 3.  Assigns the new Car object to the reference variable myCar;

think of the reference variable as a locator for the object

Page 6: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 6

Variables in Java

•  Variables are particular shaped containers (hold data)

•  Variables can hold a value (so long as it is of the right shape)

•  The shapes are called types

Java is STRONGLY typed

Type Value 1 Value 2

Page 7: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 7

Variables & Values in Java

•  Variables implemented as memory locations

•  Values implemented as 0’s & 1’s

•  Values placed in memory locations

•  The Java compiler needs to know the type of every variable - to know the values it can take and permissible operations

•  In Java, every variable must be declared before it can be used - so it can allocate space in memory

Page 8: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 8

Types in Java

•  Primitive types: –  directly represented by typical processors – the

most efficient –  begin with lowercase letter

•  Class types: –  a type for objects –  begin with UPPERCASE

letter

8 primitive types in Java, others are represented using objects – sometimes called object reference types

Page 9: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 9

A Recap on Types

•  A type defines: –  the set of values belonging to the type –  the set of operations that can be applied to the

values (e.g. the + operator can be applied to an integer, but not to a boolean)

•  Your Java program won’t compile if you try to apply invalid operators to values

Page 10: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 10

Primitive Types in Java

•  byte – 8-bit integer •  short – 16-bit integer •  int – 32-bit integer •  long – 64-bit integer •  float – 32-bit floating point •  double – 64-bit floating point •  char – 16-bit unsigned Unicode character code •  boolean – 1 bit true or false

Values are actual integers, floats, characters, etc

Integer data types – no fractions

Floating point data types –fractions

Page 11: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 11

A Way to Think of Primitives

•  We can predict these! •  Think Predict/Primitive

Page 12: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 12

Perhaps Class Types Look Like This?

•  We can’t predict these! •  Think Chaotic/Class

Page 13: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 13

Class Types in Java

•  Non primitive types exist & are important

•  Class types are abstractions created from primitive types (like a bank account, an address, etc)

•  A class type that is widely used in java is String – to represent a sequence of characters (char)

•  There is a class String that you use to implement strings, so strings are actually objects, which requires a bit of extra code to use

•  They are user-defined types

“This is a string” - strings are literal values

Page 14: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 14

Variables in Java

•  A container of sorts

•  Must have a type & a name (identifier)

•  Can hold primitives or class types

•  When they hold primitive types they hold the values directly in the container

•  When they hold class types, they hold references to objects in the container (these are called object reference variables), the objects are elsewhere

Page 15: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 15

Object Reference Variables in Java

•  An object reference variable holds bits that represent a way to access an object, it does not hold the object itself (it actually holds something that works a bit like a pointer to the address at which the object can be found)

•  Objects do not live in variables on the stack, they live on the heap

•  Object reference variables provide a way to get hold of the object on the heap

•  When you create a new object, you put a locating device in the variable container

Page 16: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 16

Pointers In Java, we are VERY concerned with references

Your object reference variable actually points to the object!

Page 17: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 17

Dog myDog = new Dog();

1.  Declare a reference variable: Dog myDog = new Dog();

3.  Create an object: Dog myDog = new Dog();

5.  Link the object & the reference: Dog myDog = new Dog();

Object Declaration, Creation & Assignment

1 2 3

Example from [Sierra & Bates 2003] - best ever!

Dog type

Dog type

myDog

myDog

Dog object

Dog object

Index cards

Page 18: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 18

Buy Why Indirection?

•  We don’t know how big myDog will be yet - so can’t pre-allocate memory!

Page 19: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 19

Type Safety

Can’t do:

Dog myDog = new Cat();

myDog can only point to a Dog object!

But we could do:

Animal myDog = new Dog();

If Dog is defined as a type of Animal. Later!

Ouch!

Page 20: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 20

Assigning Ref Variables to Ref Variables

Dog myDog = new Dog(); Dog yourDog = myDog;

Dog type

myDog

Dog object

Heap – area of memory where you can request big variable-sized boxes

Dog type

yourDog

Stack - area of memory for storing variables (small boxes of known size)

An alias

myDog.equals(yourDog)

1 object can be referenced by lots of variables

Page 21: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 21

Remember

•  The object reference variable (ORV) and the object are

2 distinct things!

Page 22: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 22

Getting Rid of a Value in a Ref Variable

myDog = null;

Dog type

myDog Dog object

Heap

Dog type

yourDog

x

A reference variable that doesn’t point to an object is called a null reference

Check for a null reference before you follow it!

Page 23: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 23

Getting Rid of a Value in a Ref Variable

yourDog = null;

Dog type

myDog Dog object

Heap

Dog type

yourDog

x

When an object is not referenced it is a candidate for garbage collection – we get to reuse the memory

Farewell poor dog…we knew him well

x

Page 24: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 24

What if you try to Follow a Null Reference?

NullPointerException

Page 25: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 25

References to Different Objects BUT of Same Class

Dog myDog = new Dog(“snowy”); Dog yourDog = new Dog(“spot”);

Dog type

myDog

Dog object name=“snowy”

Heap

Dog type

yourDog

Dog object name=“spot”

class Dog{ private String name=“”;

public Dog(String dogName){ name=dogName; } }

Page 26: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 26

Recap So Far…

•  When you declare a variable of class type, you get a container that holds an object reference

•  Object reference variables hold bits that represent a way to access an object –  doesn’t hold the object itself –  JVM uses the reference to access the object

•  Equality (==) compares values of reference variables and not the objects they reference - need to use equals() - and so now you know that is why we use this with String types!

•  When objects are passed as method arguments, the reference to the object is copied into the method’s formal parameter

… & this last point is where the fun begins!

Page 27: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 27

Placeholder … polymorphic variables

Page 28: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 28

Parameter Passing

•  Passing an object reference as a parameter to a method

•  Pass/call by value (default in Java): –  Local copy stored in method’s parameter –  Changes are only made to the local copy –  Old value remains in calling method when returns

•  Pass/call by reference (objects): –  Passes a reference to the object (i.e. its address) –  Local copy is therefore an alias for the object –  Change the object, change remains once returned from the

method, as actual parameter references the same object –  Change the object reference & make it point to a new object,

the actual parameter still refers to the original object

Trace the code in listings: 5.1-5.3 & fig 5.3 (for text ed. 3) 6.15-6.17 & fig 6.5 (for text ed. 4+) http://duke.csc.villanova.edu/jss1/examples.html

Important!

void myMethod(int number){…}

void myMethod(Dog myDog){…}

copy

ref

Do the remaining questions in Lab 2

Page 29: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 29

Special Things!

•  The null reference: a reference that doesn’t point to a valid object (watch for NullPointerException)

•  The this reference: a reference to the current executing object

•  The final reserved word – used to indicate that a variable holds a constant value (i.e. one that cannot be changed), often used when the variable is static

•  final can also be used with classes & methods … discussed when we get to inheritance

private static final int MAX = 100;

class Example{ private int i;

public int foo(int i){ this.i=i; } }

Page 30: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 30

Static Modifiers

•  Variables: –  instance variables (each object of a class gets its own copy) –  local variables (used temporarily inside a method) –  static variables (single copy shared by all instances of a

class; they are class variables)

•  Methods: –  instance methods must be called for an object of the class –  static methods are class methods & do not need to be called

for an object – if private, any method in the class can call them; if public, any other method in any class can call them

private int instanceVariable; private static int classVariable;

Examine the code in listings 5.4 & 5.5 (3rd ed.) or 6.1 & 6.2 (4th + ed.)

Try to initialise a variable as close to the point of declaration as possible

Page 31: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 31

Wrappers

•  Wrapper classes – a way to represent primitive types so they too can be treated like objects

int i = 42; Integer myInt = new Integer(i);

int unWrap = myInt.intValue()

•  Wrapper classes for each primitive type •  Helps for polymorphism - another cool thing for later!

primitive int value passed to the wrapper constructor

class type for treating ints as objects so object myInt has 42 as a private instance variable

Wrapper classes have methods to convert objects back to primitives

int

Integer

NOTE for Java 5.0+ - autoboxing & auto-unboxing

Page 32: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 32

Outer & Inner Classes

•  Outer & inner classes – safe nesting

•  We will return to this when we look at GUIs, event-driven programming & listeners in the next course

Examine the code in listings 5.7 & 5.8 (3rd ed.) or 4.10 & 4.11 (4th + ed.)

Page 33: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 33

Key Points

•  Java has primitive types & class types: –  variable of primitive type – bits represent the value

of the variable –  variable of class type (called object reference

variable) – bits represent how to access the object

•  Be aware of what you are actually doing when you pass objects as parameters to methods - it is not the same as passing primitives

Page 34: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 34

To Do…

•  Complete reading Chapter 5 of (ed 3) or Chapter 6 of (ed 4+)… & use the book website for downloads of code to experiment with. Prepare for next class by reading Chapter 6.0 & 6.1 (ed 3) or Chapter 7.1-7.3 (ed 4+)

•  Complete programming project 5.2 (ed 3) or 6.4 (ed 4+)

•  Complete LAB2 – get as far as you can … seek help if you need it

•  Go through the lecture slides again & ask me if you have any questions … don’t forget your projects!

Try PP 5.3-5.5 (ed 3) or 6.5-6.7 (ed 4+) if you have lots of spare time

Extra task – find out about autoboxing & auto-unboxing

Page 35: cs121 week12 - SEIDENBERG SCHOOL OF CSIScsis.pace.edu/~ogotel/teaching/cs121_week12_big.pdf · CS121/IS223 Week 12, Slide 30 Static Modifiers • Variables: – instance variables

CS121/IS223 Week 12, Slide 35

Coming Up Next

•  PRACTICE AND REVISION

•  More practice with OO program design - a bit more UML and putting it all together

•  Project clinics where needed – if you are working as a pair, you must tell me this week!

•  Sample exam questions