Professional Documents
Culture Documents
Objects are created while a program is running. When an object reference is assigned to a
variable, then that variable says how to find that object in memory.
In diagrams, an object reference is shown as an arrow from the object reference variable to the
object.
Two Kinds of Variables
An object reference does not contain the actual data, There are two kinds of
variables in Java:
How do you tell the two kinds of variables apart? Easy: look at how the variable is declared.
Unless it was declared to be of a primitive type, it is an object reference variable. A variable will
not change its declared type.
Question
How are the following variables declared?
class EgString2
Two Types of Use
{ System.out.println( str );
public static void main ( String[] args ) is executed, the object referred to by str is found and its data is written to the monitor. When
{
String str; the statement
long value;
System.out.println( value );
str = new String( "The Gingham Dog" );
is executed, the primitive value in the variable value is used directly. (It is translated into
value = 32912;
character form and written to the monitor.)
System.out.println( str );
System.out.println( value );
}
} There are two kinds of variables. When used in an expression (as above) they each behave
in a different way:
Here are some details about how the reference variable and the two objects relate:
Garbage
Details of the action:
Each time the new operator is used, a new object is created.
Each time an object is created, a reference to it is created.
Each object that exists in a computer system has a unique reference.
This reference is usually saved in a variable.
The reference in the variable can be used to find the object.
If another reference is saved in the variable, it replaces the previous reference.
If no variables refer to an object, there is no way to find it, and it becomes garbage.
The word garbage is the correct term for objects that are not referred to by any reference
variable. Since they cannot be found by the program, there is no reason for them to exist.
This situation is common and not usually a mistake. As a program executes, objects are
created. Then when they are no longer needed references to them are discarded. However,
a part of the Java system called the garbage collector reclaims the lost objects so that the
memory they are made of can be used again.
Several Objects of the Same Class
This program has TWO reference variables, strA and strB. It creates two objects and
places each reference in one of the variables. Since each object has its own reference
variable, no reference is lost, and no objects become garbage (until the program has
finished running).
Picture of Two Objects and Two
Reference Variable
Equality References
The == operator looks at the contents of two reference variables. If both reference variables contain the
same reference, then the result is true. Otherwise the result is false. Since object references are unique,
the == operator returns true if two reference variables refer to the same object.
The == operator does NOT look at objects!
Since the reference in strA is different than the reference in strB, strA == strB is false. The
third println() statement will not execute.
Two Reference Variables Pointing to
One Object
When this program runs, only one object is created (by the new). The unique reference to the object is
put into strA. Then the statement
strB = strA; // Copy the reference to strB.
copies the reference that is in strA into strB. It does not make a copy of the object!
Or, saying nearly the same thing: making a copy of a reference to an object does not make a copy of the
object. The same information is stored in strA and strB. Both variables refer to the same object. This is
somewhat like giving your phone number to several people: each copy of your phone number is a
reference to you, but there is only one you. The program will output:
The Gingham Dog
The Gingham Dog
Same info in each reference variable.
Picture of One Objects and Two
Reference Variables
Now when the expression strA == strB is evaluated, it is true because the contents of strA and
of strB are the same (they both contain the same reference).
String strA; // will contain the reference to the object
String strB; // another copy of the reference to the object
== reports false because there are two different objects, each with a unique reference
The equals() Method
The equals( String ) method of class String tests if two Strings contain the same characters.
The equals( String ) method looks at objects. It detects equivalence. The == operator
detects identity. For example,
In this example, there are two objects. Each object has its own identity, and its own unique
reference, so == returns false. Each object contains equivalent data, soequals() returns true.
String Literals
Strings are common in programs, so Java optimizes their use. Usually if you need a string in your
program you create it as follows. Notice that new is not used:
String str = "String Literal" ;
A string literal is an object of class String. The compiler keeps track of the literals in your program and
will reuse the same object when it can. (The compiler will not try to reuse other types of objects. String
literals are special.)
For example, say that a program contained the following statements:
String str1, str2;
str1 = "String Literal" ;
str2 = "String Literal" ;
Only one object is created, containing the characters "String Literal". This is safe, because strings are
immutable, so the object str1 refers to will not change, and the object str2 refers to will not change, so if
the data is always the same, only one object is needed to contain it. There is no need for two string
objects whose content is identical. Here is different situation:
String strA, strB;
strA = new String("My String") ;
strB = new String("My String") ;
Now two objects are created, containing identical data. The compiler does not reuse the first object.
Example
As with many optimizations, you almost wish they hadn't done it. It does confuse things. But real-world
programs often use the same message in many places (for example "yes" and "no") and it saves space
and time to have only one copy. Only rarely will you need to think about this difference.
But the difference between == and equals() is very important, and you will be sunk if you don't know
the difference.
Two Strings that are == are always
equal()
== determines if two variables refer to the same object. It is common in the real world (and in
programs) for an object to have several names, for example "Mark Twain" and "Samuel Clemens" are
two names for the same author.