Você está na página 1de 21

Strings and Object References

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");

Easy way to Construct Strings


String zeta = "The last rose of summer" ;

Question: Can a String be a parameter for a


method?

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.

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?

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

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

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
}
}

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.

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 first statement does two things: The line through the box in the
second statement symbolizes the
(1) a String object is created,
null value. The object still exists in
containing the characters "Dempster
memory. The memory occupied by
Dumpster". Then, (2) a reference to
the object will eventually be
that object is saved in the reference
recycled by the garbage collector
variable alpha
and will be made available for new
objects.

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

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?

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?

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());

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

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

Você também pode gostar