Introduction to Algorithms and Data Structures Lecture 3 References and Packages Variables and Memory Variables are stored in main memory at specific addresses that are determined when the program enters the method Variables of different types require different number of bytes to hold their data Variables and Memory An Example public static void main String args int x y byte a 2500 x 2504 y a 2508 Objects and References When an object is declared space is set aside to hold the address at which the object s data is stored e g MyClass myObject There is a location in memory where the address is stored at which all the data about myObject is contained The address that is stored is called a reference Declaring an Object MyClass myObject null Constructing an Object MyClass myObject new MyClass 2064 Data About myObject 2064 ToyClass java public class ToyClass private String name private int number public ToyClass String initialName int initialNumber name initialName number initialNumber public ToyClass name No name yet number 0 public void set String newName int newNumber name newName number newNumber public String toString return name number public static void changer ToyClass aParameter aParameter name Hot Shot aParameter number 42 public boolean equals ToyClass anotherObject return name equals anotherObject name number anotherObject number TestToyClass java public class TestToyClass public static void main String args ToyClass variable1 new ToyClass Joe 42 ToyClass variable2 variable2 variable1 Now both variables name the same object variable2 set Josephine 1 System out println variable1 Output Josephine 1 Tracing through TestToyClass ToyClass variable1 new ToyClass Joe 42 ToyClass variable2 4068 Joe 42 variable1 4068 variable2 Data About myObject Tracing through TestToyClass continued variable2 variable1 4068 Joe 42 variable1 4068 Data About myObject variable2 4068 Tracing through TestToyClass continued variable2 set Josephine 1 4068 Josephine 1 variable1 4068 variable2 4068 Data About myObject Objects As Parameters All parameters are passed by value i e the value gets copied over into the method s memory area For objects it is the reference to the object that gets copied over i e the address at which the object s data appears Any changes made to an objecct passed as a parameter are permanent the calling procedure knows about them ClassParameterDemo java An Example public class ClassParameterDemo public static void main String args ToyClass anObject new ToyClass Mr Cellophane 0 System out println anObject System out println Now we call changer with anObject as argument ToyClass changer anObject System out println anObject Output Mr Cellophane 0 Now we call changer with anObject as argument Hot Shot 42 Tracing through ClassParameterDemo Before 3078 Mr Cellophane aParameter 0 Data About anObject anObject 3078 Tracing through ClassParameterDemo continued ToyClass changer anObject 3078 Mr Cellophane aParameter 3078 anObject 3078 0 Data About anObject Tracing through ClassParameterDemo continued aParameter name Hot Shot aParameter number 42 3078 Hot Shot aParameter 3078 42 Data About anObject anObject 3078 Comparing Objects To test the equality of two objects it is always a good idea to compare them using a equals method When you compare two objects using the operator you are comparing the two references CompareEqualsDemo java public class CompareEqualsDemo public static void main String args ToyClass object1 new ToyClass Slinky 31 object2 new ToyClass Slinky 31 if object1 object2 System out println Equal using else System out println Not equal using if object1 equals object2 System out println Equal using equals else System out println Not equal using equals null null is a special constant that can be assigned to any object reference indicating that is has no real value It is also used to indicate when an object has not yet been constructed YourClass yourObject null if yourObject null System out println No real object here Anonymous Objects When I write ToyClass variable1 new ToyClass Joe 42 I am calling a constructor to create an instance of object in memory whose reference is stored in variable 1 I can write if variable1 equal new ToyClass JOE 42 System out println Equal else System out println Not equal Since I will NEVER want to use new ToyClass JOE 42 again This is an anonymous object object it has no name to identify it Anonymous Objects Are Temporary Writing if variable1 equal new ToyClass JOE 42 System out println Equal else System out println Not equal is equivalent to ToyClass temp new ToyClass JOE 42 if variable1 equal temp System out println Equal else System out println Not equal Invariants An invariant is a statement that is always true E g for i 0 i 5 i Invariant i 5 A class invariant is a statement that is always true about a class Person java Class for a person with a name and dates for birth and death Class invariant A Person always has a date of birth and if there is a date of birth it precede the date of birth public class Person private String name private Date born private Date died null indicates still alive Person This constructor initializes the Date objects uses their copy constructor public Person String initName Date birthDate Date deathDate if consistent birthDate deathDate name initName born new Date birthDate if deathDate null died null else died new Date deathDate else System out println Inconsistent dates Aborting System exit 0 Person A copy constructor for Person public Person Person original It is essential that we determine if the original object exists by seeing if its reference is null or not if original null System out println Fatal error System exit 0 name original name born new Date original born If we had written born original born it would be an example of a privacy leak if original died null Not a problem died null else died new Date original died Mutator that sets the values for the whole class public void set String newName Date birthDate Date deathDate Make sure that the dates are consistent if consistent birthDate deathDate name newName born cannot be null but died might be born new Date birthDate if died null died new Date deathDate else died deathDate toString converts a person s data into a String public String toString First let s see if there is a date of death String diedString if died null diedString Empty string else diedString died toString born is converted using Date toString return name born diedString equals Returns true if
View Full Document