Consider the following code:
Values for primitive variables such as
are stored directly in some
However objects are never stored in variables.
Instead all objects are creating using a different area of RAM
known as the heap.
Variables such as
not contain objects, but rather the RAM address of objects.
For this reason Java refers to non-primitive variables not as
object variables, but rather as object reference variables.
It may help to know something about how a compiler works. When compiling a program a compiler builds a table known as the symbol table. This table is added to for each variable seen in the program. It has columns for the name, type, and RAM location for each variable. Initially the RAM location is blank—the compiler fills in that column last.
|args||String array ref||100|
Before producing compiled output (the
.class file), the
symbol table is filled in with actual RAM addresses to use for each
Then when producing byte code the compiler replaces the name of variables
with the RAM addresses from the symbol table.
The layout of RAM for this program would look something like this:
When producing byte code for lines 5 and 6 the Java compiler produces something like:
STORE "17" into RAM location 104 STORE "17" into RAM location 108
For lines 7 and 8 of the program above, the output is different. It becomes something like:
CREATE String object in next available heap location with initial value "Joe" ⇒ Object created at RAM location 1460 STORE "1460" into RAM location 112 CREATE String object in next available heap location with initial value "Joe" ⇒ Object created at RAM location 1520 STORE "1520" into RAM location 116
So what is produced for lines 10 and 12? The results are as you might expect. For line 10:
COMPARE contents of RAM locations 104 and 108 ⇒ set result to TRUE (17 equals 17)
When the program is run you will see "
i equals j" displayed.
But you won't see "
name1 equals name2",
which surprises many.
The reason is the byte code produced for line 12 is essentially the same
as for line 10:
COMPARE contents of RAM locations 112 and 116 ⇒ set result to FALSE (1460 not equal to 1520)
Many expect Java to compare the contents of RAM locations 1460 and 1520,
but that isn't what Java does.
It is only comparing the object references, not the objects themselves.
To compare the objects and not the object references you can't use the
Instead you use the "
equals" method, something like:
if ( name1.equals(name2) )
Will cause the Java compiler to produce byte code to compare the contents
String objects in RAM locations 1460 and 1520.