Java Lect 14
Java Lect 14
Lecture # 14
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");
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.");
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
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
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)?
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.
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 );
}
}
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
}
}
Garbage
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.
Not Garbage
Tedious Review
Can an object reference variable exist without
referring to an object?
Yes
Assignment
Study carefully InsertSortApp program
Make the InsertSortApp program usable for
file input
THE END
Home: http://www.cse.iitb.ac.in/~manish
Contact:manish[at]cse[dot]iitb[dot]ac[dot]in