I have a simple question about strings in Java. The following segment of simple code just concatenates two strings and then compares them with ==
.
Though, when you create using String literal notation of Java, it automatically call intern() method to put that object into String pool, provided it was not present in the pool already.
Why does final make a difference?
Compiler knows the final variable never gonna change, when we add these final variables the output goes to String Pool because of str1 + str2
expression output also never gonna change, So finally compiler calls inter method after output of the above two final variables. In case of non-final variable compiler do not call intern method.