My question is in regard to the way Java handles String literals. It\'s quite clear from the Java Language Specs (JLS) that String literals are being implicitly interned - in ot
Seems like because you reference an object here final String stringObject = (String) object;
, this is no longer a 'compile-time' constant, but a 'run-time' constant. The first example from here eludes to it with the part:
String s = "lo";
String str7 = "Hel"+ s;
String str8 = "He" + "llo";
System.out.println("str7 is computed at runtime.");
System.out.println("str8 is created by using string constant expression.");
System.out.println(" str7 == str8 is " + (str7 == str8));
System.out.println(" str7.equals(str8) is " + str7.equals(str8));
The string str7 is computed at runtime, because it references another string that is not a literal, so by that logic I assume despite that face that you make stringObject
final, it still references an object, so cannot be computed at compile time.
And from the java lang spec here, it states:
"The string concatenation operator + (§15.18.1) implicitly creates a new String object when the result is not a compile-time constant expression (§15.28). "
I cannot find any examples where a cast can be used, except, for this terrible, terrible example:
System.out.println(hello == "hel" + ( String ) "lo");
Which hardly has any logical use, but maybe the part about a string cast was included because of the above case.