1 / 44

CMSC 150 primitive vs. class variables

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:

kathie
Télécharger la présentation

CMSC 150 primitive vs. class variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMSC 150primitive vs. class variables CS 150: Fri 10 Feb 2012

  2. 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 );

  3. 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’); } }

  4. 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’); } }

  5. 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

  6. 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() …

  7. 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() …

  8. 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() …

  9. 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() …

  10. 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() …

  11. 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() …

  12. 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() …

  13. What is the result? SimpleStringmyString; int length = myString.length(); System.out.println( length );

  14. What is the result? SimpleStringmyString; int length = myString.length(); System.out.println( length ); • Won't even compile! • But let’s move the variable declaration…

  15. What is the result?

  16. Red Text Of Death!! in other words, Java throws an “exception”

  17. The question is… WHY ?!?!

  18. 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 ); } }

  19. 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() …

  20. In this context… 127 null myString 128 129 130 131 132

  21. No object to reference… 127 null myString 128 129 130 ??? 131 132

  22. 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 ); } }

  23. 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); }

  24. 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

  25. 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"

  26. 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"

  27. 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"

  28. 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"

  29. 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"

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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!

  40. 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

  41. 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

  42. 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() …

  43. 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() …

  44. 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

More Related