1 / 21

Strings and Object References

Strings and Object References. Lecture # 14 Manish Sinha. Topics. String Review The null value Garbage Collection Strings are immutable Sorting by Insertion. Review. What two things does the following statement do? String zeta = new String("The last rose of summer");

Download Presentation

Strings and Object References

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. Strings and Object References Lecture # 14 Manish Sinha

  2. Topics • String Review • The null value • Garbage Collection • Strings are immutable • Sorting by Insertion

  3. Review • What two things does the following statement do? String zeta = new String("The last rose of summer"); • Easy way to Construct Strings String zeta = "The last rose of summer" ; • Question: Can a String be a parameter for a method?

  4. String References as Parameters • Example: Piece of a code String stringA = “Candle light"; String stringB = “Moon light"; if ( stringA.equals( stringB ) ) System.out.println("They are equal."); else System.out.println("They are different.");

  5. String References as Parameters ….. • Be CAREFUL: People often say "String" when they really mean "reference to a String" • stringA.equals(stringB) • What is usually said:The equals method of stringA is called with stringB • Careful meaning:The equals method of the String referenced by stringA is called with a reference to stringB

  6. Object Reference • Object Reference is an information on how to find a particular object • Object is a chunk of memory. A reference to object is a way to get to that memory • Objects are created while a program is running • An object reference does not contain the actual data

  7. Questions • Examine the following snippet of code. Answer the questions carefully: String a; Point b; • What is the data type of the variable a? • What is the data type of the variable b? • How many objects are there (so far)?

  8. Answers Q: What is the datatype of the variable a? A: a is a reference to a String object. Q: What is the data type of the variable b? A: b is a reference to a Point object. Q: How many objects are there (so far)? A: So far, there are no objects, just variables that can be used to keep track of objects once there are some.

  9. The null value • A reference variable holds information about the location of an object. It does not hold the object itself • A special value called null is assigned to an object reference variable when it does not refer to an object • The value null is a special value that means "no object." A reference variable is set to null when it is not referring to any object • Though Question: Do you think that null can be assigned to reference variables of any type?

  10. nullDemo1.java class nullDemo1 { public static void main (String[] arg) { String a = “Amitabh Bachchan"; String b = null; String c = ""; if ( a != null ) System.out.println( a ); if ( b != null ) System.out.println( b ); if ( c != null ) System.out.println( c ); } }

  11. Null Assigned to any Reference variable • Question: In nullDemo1.java, What exactly is variable c initialized to? • Answer: c is initialized to a reference to a String object containing no characters. This is most certainly a different value than null • We need a universal value that means “nothing here”, the value null can be assigned to any reference variable • A reference variable sometimes does and sometimes does not refer to an object. You need a way to say that a variable does not now refer to an object. You do this by assigning null to the variable

  12. nullDemo1.java class nullDemo1 { public static void main (String[] arg) { String a = “Amitabh Bachchan"; // 1. an object is created // variable a refers to it String b = null; // 2. variable b refers to no object String c = ""; // 3. an object is created // containing no characters ----- variable c refers to it if ( a != null ) // 4. ( a != null ) is true, so System.out.println( a ); // 5. the println( a ) executes. if ( b != null ) // 6. ( b != null ) is false, so System.out.println( b ); // 7. the println( b ) is skipped. if ( c != null ) // 8. ( c != null ) is true, so System.out.println( c ); // 9. the println( c ) executes. // but it has no characters to print } }

  13. The Empty String • A String object that contains no characters is still an object. Sometimes such an object is called an empty string. • Examine the following code snippet: String alpha = "Dempster Dumpster"; alpha = null; . . . • Where is an object constructed? • In the first statement. • What becomes of that object? • It became garbage in the second statement.

  14. Garbage • The second statement assigns the value null to alpha. When this happens, the reference to the object is lost. Since there is no reference to the object elsewhere, it is now garbage. • The line through the box in the second statement symbolizes the null value. The object still exists in memory. The memory occupied by the object will eventually be recycled by the garbage collector and will be made available for new objects. • The first statement does two things: (1) a String object is created, containing the characters "Dempster Dumpster". Then, (2) a reference to that object is saved in the reference variable alpha

  15. Garbage… • String alpha = "Dempster Dumpster"; String beta = alpha; alpha = null; • When was an object instantiated? In the first statement. • What happened in the second statement?   The reference to the object was copied to beta. • What becomes of beta object?   It remains "alive," because there is still a reference to it.

  16. Not Garbage • This time, the object does not become garbage. This is because in the second statement a reference to the object is saved in a second variable, beta. Now when, in the third statement, alpha is set to null, there is still a reference to the object. • There can be many copies of the reference to an object. Only when there is no reference to an object anywhere does the object become garbage. • Review: How can you determine what an object of a particular class can do?

  17. String are immutable • Strings are forever • Confusion Alert: Distinguishing String Object and String Object reference • Inspect the following code: String ring = "One ring to rule them all, " ; String find = "One ring to find them." ; ring = ring + find; • Does the last sentence violate the rule that Strings are immutable?

  18. Are the following statements True? • String line = "The Sky was like a WaterDrop"; • String a = line.toLowerCase(); • String b = toLowerCase(line); • String c = toLowerCase( "IN THE LIGHT OF MOON"); • String d = “Neat, Clear, Beautiful".toLowerCase(); • System.out.println("Dark,stormy...".toLowerCase());

  19. Tedious Review • Can an object reference variable exist without referring to an object? • Yes, a reference variable can be declared without initialization: String myString; • Also, a reference can be set to null. • Can an object exist without an object reference variable referring to it? • Yes

  20. Assignment • Study carefully InsertSortApp program • Make the InsertSortApp program usable for file input

  21. THE END Home: http://www.cse.iitb.ac.in/~manish Contact:manish[at]cse[dot]iitb[dot]ac[dot]in

More Related