440 likes | 566 Views
CMSC 150 primitive vs. class variables. CS 150: Fri 10 Feb 2012. Primitive vs. Class Variables. primitive type variables: value of interest stored directly in memory cell use assignment statement to set value reference (class) type variables:
E N D
CMSC 150primitive vs. class variables CS 150: Fri 10 Feb 2012
Primitive vs. Class Variables • primitive type variables: • value of interest stored directly in memory cell • use assignment statement to set value • reference (class) type variables: • stores memory addy where object of interest resides • use construction intanswerToLife = 42; double pi = 22.0 / 7.0; // not really Integer myInteger = new Integer( 42 );
In Memory jenny 8675309 public class StringThing { public static void main(String[] args) { int jenny = 8675309; booleangrammasTeeth= false; double pi = 3.14159; SimpleStringstr = new SimpleString(‘H’,’i’); } }
In Memory jenny 8675309 grammasTeeth false public class StringThing { public static void main(String[] args) { int jenny = 8675309; booleangrammasTeeth= false; double pi = 3.14159; SimpleStringstr = new SimpleString(‘H’,’i’); } }
In Memory jenny 8675309 grammasTeeth false public class StringThing { public static void main(String[] args) { int jenny = 8675309; booleangrammasTeeth= false; double pi = 3.14159; SimpleStringstr = new SimpleString(‘H’,’i’); } } pi 3.14159
In Memory jenny 8675309 grammasTeeth false public class StringThing { public static void main(String[] args) { int jenny = 8675309; booleangrammasTeeth= false; double pi = 3.14159; SimpleStringstr = new SimpleString(‘H’,’i’); } } pi 3.14159 str (memaddr) myFirstChar ‘H’ mySecondChar ‘i’ 2 myLength char chartAt(int index) int length() void printString() …
What is the result? myString 0x125BD SimpleStringmyString = new SimpleString(‘Z’,’a’); myFirstChar ‘Z’ mySecondChar ‘a’ 2 myLength char chartAt(int index) int length() void printString() …
What is the result? myString 0x125BD yourString 0x125BD SimpleStringmyString = new SimpleString(‘Z’,’a’); SimpleStringyourString = myString; myFirstChar ‘Z’ mySecondChar ‘a’ 2 myLength char chartAt(int index) int length() void printString() …
What is the result? myString 0x125BD yourString 0x125BD SimpleStringmyString = new SimpleString(‘Z’,’a’); SimpleStringyourString = myString; myFirstChar ‘Z’ mySecondChar ‘a’ 2 myLength char chartAt(int index) int length() void printString() …
What is the result? myString 0x125BD yourString 0x125BD SimpleStringmyString = new SimpleString(‘Z’,’a’); SimpleStringyourString = myString; SimpleStringourString = new SimpleString(‘Z’,’a’); ourString 0x18A2D myFirstChar ‘Z’ mySecondChar ‘a’ 2 myLength char chartAt(int index) int length() void printString() …
myString 0x125BD myFirstChar ‘Z’ mySecondChar ‘a’ yourString 2 0x125BD myLength char chartAt(int index) int length() void printString() … ourString 0x18A2D myFirstChar ‘Z’ mySecondChar ‘a’ 2 myLength char chartAt(int index) int length() void printString() …
myString 0x125BD myFirstChar ‘Z’ mySecondChar ‘a’ yourString 2 0x125BD myLength char chartAt(int index) int length() void printString() … ourString 0x18A2D myFirstChar ‘Z’ Notice the two objects contain the “same” data, but are two distinct objects at separate locations in memory. Each object gets its own copies of data & methods. mySecondChar ‘a’ 2 myLength char chartAt(int index) int length() void printString() …
What is the result? SimpleStringmyString; int length = myString.length(); System.out.println( length );
What is the result? SimpleStringmyString; int length = myString.length(); System.out.println( length ); • Won't even compile! • But let’s move the variable declaration…
Red Text Of Death!! in other words, Java throws an “exception”
The question is… WHY ?!?!
First, try printing the object… public class StringThing { static SimpleStringmyString; // instance variable public static void main(String[] args) { // int length = myString.length(); // System.out.println( length ); System.out.println( myString ); } }
Recall from before… • SimpleStringaPhrase; • aPhrase = new SimpleString(‘H’,’i’); 127 aPhrase 128 130 129 • variable references the actual SimpleStringobject which resides elsewhere in memory 130 ‘H’ ‘i’ 131 2 132 char chartAt(int index) int length() void printString() …
In this context… 127 null myString 128 129 130 131 132
No object to reference… 127 null myString 128 129 130 ??? 131 132
Printing a Class-Type Variable When you print a class-type variable, Java will automatically call the toString() method (if it exists) in that class. If the variable is not null, equivalent to System.out.println( myString.toString() ); public class StringThing { static SimpleStringmyString; // instance variable public static void main(String[] args) { // int length = myString.length(); // System.out.println( length ); System.out.println( myString ); } }
A String Example public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); }
A String Example public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } true false true false false
A String Example public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } These are "equal"
A String Example public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } These are not "equal"
A String Example public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } These are "equal"
A String Example public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } These are not "equal"
A String Example public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } These are not "equal"
Why? References public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } "str0" 127 "str1" 128 "str2" 129 130 131 132
Why? Compiler recognizes these as the same constant public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } "str0" 127 "str1" 128 "str2" 129 130 131 132
Why? Stores only one copy in memory public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } "str0" 127 "str1" 128 "str2" 129 130 131 "Lilly" 132
Why? Stores the start address of the String in each variable public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } 132 "str0" 127 "str1" 128 132 "str2" 129 132 130 131 "Lilly" 132
Why? str0and str1 reference the same String public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } 132 "str0" 127 "str1" 128 132 "str2" 129 132 130 131 "Lilly" 132
Why? public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } 132 "str0" 127 "str1" 128 132 "str2" 129 132 compares the address values! "Lilly" 132
Why? public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } 132 "str0" 127 "str1" 128 132 "str2" 129 132 EQUAL! "Lilly" 132
While elsewhere in memory… public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } 149 "str3" 145 146 149 "Lilly" 150
While elsewhere in memory… public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } 149 "str3" 145 "str4" 146 150 149 "Lilly" "Lilly" 150
While elsewhere in memory… public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } 149 "str3" 145 "str4" 146 150 149 "Lilly" "Lilly" 150 compares the address values!
While elsewhere in memory… public class ObjectTest { private String str0 = "Lilly"; public ObjectTest() { String str1 = "Lilly"; String str2 = "Lilly"; String str3 = new String("Lilly"); String str4 = new String("Lilly"); System.out.println(str0 == str1); System.out.println(str0 == str3); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str3 == str4); } 149 "str3" 145 "str4" 146 150 149 "Lilly" NOT EQUAL! "Lilly" 150
Moral of the Story • Never compare equality of reference variables using == • You will be comparing memory addresses • Not comparing contents of the actual objects • Use .equals() method
What is the result? myString 0x125BD SimpleStringmyString = new SimpleString(‘Z’,’a’); SimpleStringyourString = myString; SimpleStringourString = new SimpleString(‘Z’,’a’); yourString 0x125BD ourString 0x18A2D myFirstChar ‘Z’ mySecondChar ‘a’ 2 myLength char chartAt(int index) int length() void printString() …
What is the result? myString 0x125BD SimpleStringmyString = new SimpleString(‘Z’,’a’); SimpleStringyourString = myString; SimpleStringourString = new SimpleString(‘Z’,’a’); yourString 0x125BD ourString 0x18A2D myFirstChar ‘Z’ (myString == yourString) (myString == ourString) (myString.equals(yourString)) (myString.equals(ourString)) mySecondChar ‘a’ 2 myLength char chartAt(int index) int length() void printString() …
What is the result? myString 0x125BD SimpleStringmyString = new SimpleString(‘Z’,’a’); SimpleStringyourString = myString; SimpleStringourString = new SimpleString(‘Z’,’a’); yourString 0x125BD ourString 0x18A2D myFirstChar ‘Z’ (myString == yourString) (myString == ourString) (myString.equals(yourString)) (myString.equals(ourString)) TRUE mySecondChar ‘a’ FALSE 2 myLength TRUE char chartAt(int index) int length() void printString() … TRUE