2013年9月16日星期一

Java string memory address

String ss1 = "a" + "bc";
String ss2 = "abc";
System.out.println (ss1 == ss2) ;/ / true

ss1 = ss1 + "def";
ss2 = "abc" + "def";
System.out.println (ss1 == ss2) ;/ / false --- Why ?
------ Solution ---------------- ----------------------------
do not know ah wait cattle Nikkei
------ Solution --------------------------------------------
seemingly ss1 = ss1 + "def"; this line has a variable equal right reason . . .
------ Solution ---------------------------------------- ----
indeed a bit Nikkei .

String The Flyweight pattern , at compile time constants can be determined immediately , in order better to be effective . And now this effect is already after the compiler optimization ; remember correctly , long ago in Java, first we are unable true.

ss1 = ss1 + "def"; run only know ss1 content , the compiler is not smart enough to infer ss1 could be here when Gesha .

------ Solution ------------------------------------ --------
compile the same string will be able to determine the point with a string constant pool
What kind of compile-time can determine it ?
that is initialized by a constant string

ss1 = ss1 + "def";
right side variables , so the compiler can not determine the period

But again, this problem does not require research and expertise
java implementation of this had to change , there is no guarantee the future does not change
understand this problem does not make any sense
------ Solution ----------------------------- ---------------


afraid to ask questions such as interviews . .
------ Solution ---------------------------------------- ----
== key is to understand the meaning .
== and equal do not use the wrong
------ Solution ---------------------------- ----------------
compile the same string will be able to determine the point with a string constant pool

------ Solution ------------------------------------ --------
compile the same string will be able to determine the point with a string constant pool

learning
------ For reference only --------------------------------- ------

------ For reference only --------------------------------------- < br> 1, the string is placed on static code region , beginning in the class is loaded when this constant is initialized in this area, and is globally shared by all access points directly to him .
beginning ss1 already exists abc a
When you first create a reference when he went back there in the static code region without this constant.
If there is, the same , they point to the same memory address.
2, the second time due to memory address has changed , so the equals false
"==" is more stack memory address is the same as

------ For reference only ---------------------------------- -----
first good explanation , are on the same stack contents ! Second , because you used the ss1 = ss1 + "def", so this time the ss1 has been first converted to an object , and then create a new virtual machine new String ("def"); Then and ss1 phase plus , the result is a string object , this time ss1 has been placed inside the heap ! And ss2 just string together a general nature or on stacks inside, this time not the same place where , in comparison with the equal sign is certainly a false

没有评论:

发表评论