Sandra Bachan wrote:
Would really appreciate if someone can draw how this looks in memory, esp when s2 is final vs. non-final.
It's not a memory issue. It's a compiler issue.
A final string referenced that is assigned during declaration to a string literal is a compile time constant. So... The s2 reference is a compile time constant. The sum of two compile time constants is a compile time constant. Hence, the assignment of the s3 reference is to a string that is a compile time constant, and hence, in the string pool. So... the first comparison is true.
While the s4 reference is pointing to a string that is in the string pool, the concat being done to be assigned the s5 reference is not done at compile time, and is not in the string pool. And hence... the second comparison is false.
I may be missing something but did not understand it fully
Henry sir said that the addition of two compile time constants is a compile time constant
s2 is a compile time constant and not the literal "C"
then how can s2 + "C" be the compile time constant?
doesn't the addition s2+"C" occur at runtime?
how does s3 become a compile time constant?
s3 = s2 + "C"
SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2012
OCEJPAD [83%] September 6th, 2014
If you find any post useful, click the "plus one" sign on the right