Reputation: 301
all, i faced a problem when a write the code below
String hello = "Hello";
String str5 = "Hel" + "lo";
String str8 = "Hel";
String str9 = "lo";
String str10 = str8 + str9;
System.out.println("str10==hello?" + (str10 == hello));
System.out.println("str5==hello?" + (str5 == hello));
System.out.println("str10==str5?" + (str10 == str5));
then i run my code and the console print this
str10 == hello ? false
str5 == hello ? true
str10 == str5 ? false
this confused me a lot. why the second print TRUE but the first print FALSE??
in my comprehension of String literal pool,when a string defined and JVM will check if the pool contains that string,if not ,put the string into the pool.
in my code,variable hello exists in string pool,"Helo" and "lo" also in the pool,my question is
my jdk version :1.6.0_29
my IDE:Intellij Idea 11.2
anyone can point it out? thank you very much
Upvotes: 5
Views: 1863
Reputation: 29
String hello = "Hello"; // at compile time string is known so in String Constant Pool
String str5 = "Hel" + "lo"; // at compile time string is known so in String Constant Pool same object as in variable hello
String str8 = "Hel"; // at compile time string is known so in String Constant Pool
String str9 = "lo"; // at compile time string is known so in String Constant Pool
String str10 = str8 + str9; // at runtime don't know values of str8 and str9 so in String Constant Pool new object different from variable hello
str10 == hello ? false // as str10 has new object and not the same as in hello
str5 == hello ? true // both were created at compile time so compiler know what's the result in str5 and referenced the same object to str5 as in hello
str10 == str5 ? false // str10 is a different object, hello and str5 are referenced same object as created at compile time.
Upvotes: 1
Reputation: 1979
The code has the following points to consider:
String hello = "Hello";
Here "Hello" is a literal assigned to reference hello thus the literal has its own hashcode
String str5 = "Hel" + "lo";
Here "Hel"+"lo" are 2 literals combined and assigned to reference hello thus the new literal is same as the first one and thus same hashcode
String str8 = "Hel";
String str9 = "lo";
Here str8 + str9 are 2 references which combine and point to a new reference hello thus the new literal has its own hashcode
String str10 = str8 + str9;
System.out.println("str10==hello?" + (str10 == hello));
System.out.println("str5==hello?" + (str5 == hello));
System.out.println("str10==str5?" + (str10 == str5));
when you use == it matches by hash code and value. thus the mismatch.
try to use
string_1.equals(string_2)
instead of
string_1 ==string_2
and you will get value matching only. thus all true
Please refer the below answer also(from What is the difference between == vs equals() in Java?):
The equals() method compares the "value" inside String instances (on the heap) irrespective if the two(2) object references refer to the same String instance or not. If any two(2) object references of type String refer to the same String instance then great! If the two(2) object references refer to two(2) different String instances .. it doesn't make a difference. Its the "value" (that is: the contents of the character array) inside each String instance that is being compared.
On the other hand, the "==" operator compares the value of two object references to see whether they refer to the same String instance. If the value of both object references "refer to" the same String instance then the result of the boolean expression would be "true"..duh. If, on the other hand, the value of both object references "refer to" different String instances (even though both String instances have identical "values", that is, the contents of the character arrays of each String instance are the same) the result of the boolean expression would be "false".
Upvotes: 0
Reputation: 1882
If u compare two strings use string.equals
not string1 == string2
try it:
System.out.println("str10==hello?" + (str10.equals(hello));
Upvotes: -1
Reputation: 328598
It behaves as it should. It is adressed in two sections of the JLS.
strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern.
JLS #15.28 lists what is considered as a constant expression. In particular, string literals are constant expressions ("Hel" and "lo") but for a variable to be considered a constant, it needs to be final.
In your case, if you change your code slightly to make str8
and str9
constant, you will get true
three times:
final String str8 = "Hel";
final String str9 = "lo";
Upvotes: 7