You are on page 1of 27

Object References

Primitive data types, and objects


Object references and object reference variables
Copying object references
The == operator and object references
The equals() method for strings
String literals
Assignment
1. Differentiate between Object references and object
reference variables
2. Describe what is Copying object references.
3. Identify the differences between == operator and object
references
4. How an equals() method for strings works?
5. What is String literals?
Review
Primitive DataTypes
long value = 18234;
 primitive data types, a variable is a section of memory
reserved for a value that uses that data type. For example by
saying long value, 64 bits of memory are reserved for an
integer. By saying
int sum, 32 bits of memory are reserved an integer.
 Object reference variables do not work this way
A String Object
 Objects are big, complicated, and vary in size.You DO NOT automatically get an object
when you declare an object reference variable. All you get is a place to put a future
object reference

An object contains data and methods (state and behavior).


class EgString You can visualize the String object in the example
{ program like this:
public static void main ( String[] args )
{
String str;
str = new String( "The Gingham Dog" );
System.out.println( str );
}
}
Two Step with An Assignment Operator
1) The expression on the right of the = is evaluated.
2) The result of evaluation is assigned to the variable on the left of the =.
When an object is constructed, these steps happen like this:

There are three things involved in this statement: the object, a


reference to the object, and the variable.
Object Reference
An object is constructed out a chunk of main memory and hence has a unique location. An object
reference describes the location in memory of a particular object. In the picture, the
variable str contains a reference to the object.

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:

When the statement


System.out.println( str );

is executed, the reference in str is used to find the object


and to get the data to be printed.
Two Types of Assignment Statements
 There is a difference between the statements:
value = 32912;
and
str = new String( "The Gingham Dog" );
In the first statement, value is a primitive type, so the assignment statement puts the data directly
into it. In the second statement, str is an object reference variable (the only other possibility) so
a reference to the object is put into that variable.
There are only primitive variables and object reference variables, and each contains a specific
kind of information:

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:

When you declare a variable, you say what type it is.


For example:String str; says that str is a reference variable expected to contain a reference to an object of
type String. The declaration long value; says that value is a variable containing the primitive data type long.
When you compile a program, the declarations tell the compiler what style of information is kept in each
variable, so the compiler uses each variable in the appropriate way every time it is mentioned in your program.
Two Objects
class EgString3
{
public static void main ( String[] args )
{ The program does exactly what you would
String str; expect. It writes out:
The Gingham Dog
str = new String("The Gingham Dog");
System.out.println(str); The Calico Cat
str = new String("The Calico Cat");
System.out.println(str);
}
}
Reference Variable Reused
Here is a diagram that shows the one reference variable str and the two objects. The variable can
refer to only one object at a time. It first refers to the dog object, and then to the cat object.

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

strA = new String( "The Gingham Dog" );


System.out.println( strA );
strB = strA; System.out.println( strB );
if ( strA == strB )
System.out.println( "Same info in each reference variable." );
When two reference variables refer to the same object, the == operator will evaluate to true.
== Looks only at Variables
For primitive types, also, the == operator looks only at the
variables. For example:
int x = 32;
int y = 48;
if ( x == y ) System.out.println("They are equal.");
Only the contents of the variables x and y are examined. But
with primitive types, the contents of a variable is the data, so
with primitive types == looks at data.
With reference types, == looks at the contents of the variables,
but now the variables contain object references.
Two Objects with Equivalent Contents
Recall that objects have identity, state, and behavior. There are two objects, each a unique entity.
"Identity" means that each object is a unique entity, no Each object happens to contain data equivalent
matter how similar it is to another. Here is a program that to that in the other. Each object consists of a
shows this situation: section of main memory separate from the
memory that makes up the other object. The
variable strA contains a reference to the first
object, and the variable strB contains a
reference to the second object.
Since the information in strA is different from
the information in strB,
( strA == strB )
is false, just as it was in a previous program
where there were two objects. Since there are
two objects, made out of two separate sections
of main memory, the reference stored in strA is
different from the reference in strB. It doesn't
matter that the data inside the objects looks the
same.
Picture of Equivalent Data
Here is a picture of this situation. Even though both objects have equivalent data inside of
them, (strA == strB) is false.

== 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.

Consider two strings:


String strA = new String ("The Gingham Dog");
String strB = strA;

Since there is only one object, strA == strB is true.


Since both reference variables point to an object with the same data, strA.equals( strB ) is true.

If == is true, then so does equals().


Example

You might also like