Java String

String b = "abcd"
String a = "a"
String c = "bcd"
String d=a+c
system.out.println (dashed constants) / / false
Why false should be true according to the principle that there is only one copy of the java string constant.
what does the memory diagram look like here

Mar.24,2022

String needs to use the equals method when comparing values. B.equals (d) returns the reference address of the string "abcd" as the object of true,b, while d is a new object after the splicing of "a" and "bcd". The reference addresses of the two are different, so = = what is returned is the magic of the false, memory diagram, and I am also a good programmer.


JAVA 

when can this kind of virtual machine optimization be regarded as a principle? Oh, my God, who gave you that guarantee?

String d = a + c;
The

assignment is, of course, . is calculated when this sentence is executed. It calculates a new variable, not the one in the string constant pool.
speaking, there is also an optimization problem. string concatenation is optimized to be similar to the StringBuilder operation .
if you have to say, why constant calculations can be calculated when they are not compiled directly , then we are done.
this is a question of program correctness , not a matter of optimization.


first of all, it is clear that the reference data type = = compares the address value, and APCC is the StringBuffer in action
dabilb is false because it actually creates a StringBuffer object, and then uses the StringBuffer object to execute the append method to create the string object "abcd", call toString, and then convert it to String. But the converted String object, which creates the object, is put in the heap to point to the reference to the memory address, while c is a string constant, so of course it is not equal, ah, the key point is to get the string string by adding string.

Menu