问题
Beware: I'm not trying to compare if the characters are equals. Because I know how to use the String.equals() method. This question is about String reference
I was studying for the OCA exam when I started to learn about the class String and it properties as immutability, etc. According to what I read or may understand about String pool is that when a string is created Java stores this object on what they call String pool and if a new string is created with the same value it is going to make reference to the string on the String pool except is the case we use the new keyword as this creates a new reference even both string contains the same value.
For example:
String a = "foo";
String b = "foo";
String c = new String("foo");
boolean ab = (a == b); // This return true
boolean ac = (a == c); // This return false
To be clear what this code is making is on the first line of code it will create the String a = "foo"
and store this on the String pool, and on the second line of code it will create the String b
and reference to "foo"
because this already exist on the String pool. But line 3 will create a new reference of this string no matter if this already exist. Here is a little graphic example about what is happening:
The problem comes on the followings lines of code. When the string is created by concatenation does java make something different or simple == comparator have another behaviour ?
Example A:
String a = "hello" + " world!";
String b = "hello world!";
boolean compare = (a == b); // This return true
Example B:
a = "hello";
b = "hel" + "lo";
compare = (a == b); // This return true
Example C:
a = "Bye";
a += " bye!";
b = "Bye bye!";
compare = (a == b); // This return false
To watch the code running: (http://ideone.com/fdk6KL)
What is happening ?
EDIT
Fixing error on the Example B:
b = 'hel' + 'lo'
Adding clarification about the problem. It's not a comparison problem cause I know the use of
String.equals()
the problem is on the reference on the String pool
回答1:
"When the string is created by concatenation does java make something different or simple == comparator have another behaviour?"
No it does not change its behavior, what happens is that:
When concatenating two string literals "a" + "b"
the jvm joins the two values and then check the string pool, then it realizes the value already exists in the pool so it just simply assign this reference to the String. now in more details:
Look at the compiled bytecode below of this simple program:
public class Test {
public static void main(String... args) {
String a = "hello world!";
String b = "hello" + " world!";
boolean compare = (a == b);
}
}
First the JVM loads the string "hello world! and then push it to string pool (in this case) and then loads it to the stack (ldc = Load constant) [see point 1 in Image]
Then it assign the reference created in the pool to the local variable (astore_1) [see point 2 in Image]
Notice that the reference created in the string pool for this literal is #2 [See point 3 in Image]
The next operation is about the same: in concatenates the string, push it to the runtime constant pool (string pool in this case), but then it realizes a literal with the same content already exists so it uses this reference (#2) and assign in to a local variable (astore_2).
Thus when you do (a == b) is true because both of them are referencing to the string pool #2 which is "hello world!".
Your example C is kind of different tho, because you're using the += operator which when compiled to bytecode it uses StringBuilder to concatenate the strings, so this creates a new instance of StringBuilder Object thus pointing to a different reference. (string pool vs Object)
来源:https://stackoverflow.com/questions/44037516/how-java-string-pool-works-when-string-concatenation