Java Reference
In-Depth Information
When Java encounters the string literal
"Hello"
in the program, it tries to find a match in the string pool. If there
is no
String
object with the content
"Hello"
in the string pool, a new
String
object with
"Hello"
content is created
and added to the string pool. The string literal
"Hello"
will be replaced by the reference of that new
String
object in
the string pool. Because you are using the
new
operator, Java will create another string object on the heap. Therefore,
two
String
objects will be created in this case. Consider the following code:
String str1 = new String("Hello");
String str2 = new String("Hello");
How many
String
objects will be created by this code? Suppose when the first statement is executed,
"Hello"
was not in the string pool. Therefore, the first statement will create two
String
objects. When the second statement
is executed, the string literal
"Hello"
will be found in the string pool. This time,
"Hello"
will be replaced by the
reference of the already existing object in the pool. However, Java will create a new
String
object because you are
using the
new
operator in the second statement. The above two statements will create three
String
objects assuming
that
"Hello"
was not there in the string pool. If
"Hello"
was already in the string pool when these statements started
executing, only two
String
objects will be created.
Consider the following statements:
String str1 = new String("Hello");
String str2 = new String("Hello");
String str3 = "Hello";
String str4 = "Hello";
What will be the value returned by
str1 == str2
? It will be
false
because the
new
operator always creates a new
object in memory and returns the reference of that new object.
What will be the value returned by
str2 == str3
? It will be
false
again. This needs a little explanation. Note that
the
new
operator always creates a new object. Therefore,
str2
has a reference to a new object in memory. Because
"Hello"
has already been encountered while executing the first statement, it exists in the string pool and
str3
refers
to the
String
object with content
"Hello"
in the string pool. Therefore,
str2
and
str3
refer to two different objects
and
str2 == str3
returns
false
.
What will be the value returned by
str3 == str4
? It will be
true
. Note that
"Hello"
has already been added to
the string pool when the first statement was executed. The third statement will assign a reference of a
String
object
from the string pool to
str3
. The fourth statement will assign the same object reference from the string pool to
str4
. In other words, both
str3
and
str4
are referring to the same
String
object in the string pool. The
==
operator
compares the two references; therefore,
str3 == str4
returns
true
.
Consider another example.
String s1 = "Have" + "Fun";
String s2 = "HaveFun";
Will
s1 == s2
return
true
? Yes, it will return
true
. When a
String
object is created in a compile-time constant
expression, it is also added to the string pool. Since
"Have" + "Fun"
is a compile-time constant expression, the
resulting string,
"HaveFun"
, will be added to the string pool. Therefore,
s1
and
s2
will refer to the same object in the
string pool.
All compile-time constant string literals are added to the string pool. Consider the following examples to clarify
this rule:
final String constStr = "Constant"; // constStr is a constant
String varStr = "Variable"; // varStr is not a constant
// "Constant is pooled" will be added to the string pool
String s1 = constStr + " is pooled";