If i write
String s1 = "True";
String s2 = "True";
s1==s2 will return true. During compiletime, as i understand it, a 'String pool' is created, where strings which are created this way ends up. When creating s2 the compiler will check the string pool for said string, and if such string exist, point towards the same one.
String s3 = new String("False");
String s4 = new String("False");
s3==s4 will return false. Objects created with the new command will be created in heap space. So these generates two objects and point towards different strings, despite their ''contents'' being identical. (s3.equals(s4) would return true).
Now, this is what i don't understand.
String s5 = "False";
String sx = ""F";
String s6 = sx + "alse";
this returns false, and i do not understand why. These strings should all be created during compiletime, and end up in the string pool, no? s5 should generate an object in the stringpool, sx should generate an object in stringpool. This i am certain of, but how exactly does s6 create its object?
As i understand it, concatenation of string literals occurs during compiletime, so for example String s1 = "True"; String s2 = "T" + "rue"; s1==s2 returns true, since jvm checks the stringpool for "True".
Thanks!
edit: This question differs because the threads i checked did not address the situation where i first create two strings and create a third one by using one of the two previous reference and another string, such as sx + "alse" above.