Download presentation
Presentation is loading. Please wait.
Published byNadia McKenna Modified over 9 years ago
1
Object Reference
2
Variables: Class Type vs. Primitive Type What does a variable hold? –primitive type value of the variable –class type memory address (reference) of the object –not the value(s) of the object –objects generally do not have a single value and they also have methods, so what’s its "value?”
3
Post Office Analogy
4
What’s the pink card for and why?
5
Mailboxes and Java Letters: smaller, standard sizes (primitive type objects) Packages: larger, no standard sizes (class type objects) Mailbox (variable): –Primitive type: Letter itself (value/content of a primitive type object) –Class type: Pink card to get the package (reference/pointer/address of a class type object)
6
Advantages of separate treatment Mailboxes are tightly packed and well organized (primitive types) –Efficient access and storage Packages are not as well organized (classes types) –Less efficient access and storage Different memory segments for primitive type objects (mailboxes) and class types objects (back of the mailroom) Easier to move a package or a pink card around? –Parameter passing—faster to pass an address than a class type object –Returning from methods
7
Allocating Memory for a Reference and an Object A declaration such as SpeciesFourthTry s; creates a variable s that can hold a memory address (reference). A statement such as s = new SpeciesFourthTry(); allocates memory for an object of type SpeciesFourthTry and assign its memory address to variable s.
8
Issues with Class Type Variables Assignment (=) Equality (==) Parameter passing
9
Assignment with Variables of a Class Type klingon.set(“Klingon ox”, 10, 15); earth.set(“Black rhino”, 11, 2); earth = klingon; earth.set(“Elephant”, 100, 12); System.out.println(“earth:”); earth.writeOutput(); System.out.println(“klingon:”); klingon.writeOutput(); What will the output be? (see the next slide)
10
Assignment with Variables of a Class Type klingon.set(“Klingon ox”, 10, 15); earth.set(“Black rhino”, 11, 2); earth = klingon; earth.set(“Elephant”, 100, 12); System.out.println(“earth:”); earth.writeOutput(); System.out.println(“klingon:”); klingon.writeOutput(); earth: Name = Elephant Population = 100 Growth Rate = 12% klingon: Name = Elephant Population = 100 Growth Rate = 12% Output: What will the output be? klingon and earth both print Elephant. Why do they print the same thing? (see the next slide)
11
Assignment with Variables of a Class Type klingon.set(“Klingon ox”, 10, 15); earth.set(“Black rhino”, 11, 2); earth = klingon; earth.set(“Elephant”, 100, 12); System.out.println(“earth:”); earth.writeOutput(); System.out.println(“klingon:”); klingon.writeOutput(); Why do they print the same thing? The assignment statement makes earth and klingon refer to the same object. When earth is changed to “ Elephant ”, klingon is changed also. earth klingon Black rhino 11 2 Klingon ox 10 15 Before the assignment statement, earth and klingon refer to two different objects. earth klingon Klingon ox 10 15 After the assignment statement, earth and klingon refer to the same object.
12
Variables of a Class Type
13
Assignment with Variables of a Class Type Aliases –Multiple class variables that have the same memory address –They point to the same object Species mouse = new Species(“Mouse”, 10, 5); Species cat = mouse; Species lion = cat; // lion and cat are aliases of mouse
14
Comparing Class Variables A class type variable –memory address of the object Equality operator == with two class variables –the addresses of the objects are compared! –not the content of the objects –rarely what you want to do! Use the class’s equals() method to compare the content of objects referenced by class variables
15
Example: Comparing Class Variables Use equals() method (not the double-equals sign) to compare class variables //User enters first string String firstLine = keyboard.nextLine(); //User enters second string String secondLine = keyboard.nextLine(); if(firstLine == secondLine) //this compares their addresses { } if(firstLine.equals(secondLine)) //this compares their values { }
16
== with Class Type Variables
19
Programming Example
20
Programming Example, contd.
21
Class Types as Method Parameters class variable names used as parameters in a method call –copy the address in the argument to the formal parameter –formal parameter name contains the address of the argument –the formal parameter name is an alias for the argument name Any action taken on the formal parameter is actually taken on the original argument! Different for parameters of primitive types –the original argument is not protected for class types!
22
Class Parameters, cont. Example if (s1.equals(s2)) … public boolean equals(Species otherObject) causes otherObject to become an alias of s2, referring to the same memory location, which is equivalent to otherObject = s2;
23
Example: Class Type as a Method Parameter The method call makes otherObject an alias for s2, therefore the method acts on s2, the DemoSpecies object passed to the method! This is unlike primitive types, where the passed variable cannot be changed. //Method definition with a DemoSpecies class parameter public void makeEqual(DemoSpecies otherObject) { otherObject.name = this.name; otherObject.population = this.population; otherObject.growthRate = this.growthRate; } //Method invocation DemoSpecies s1 = new DemoSpecies("Crepek", 10, 20); DemoSpecies s2 = new DemoSpecies(); s1.makeEqual(s2); // s2 is changed!
24
Comparing Class Parameters and Primitive-Type Parameters, cont.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.