I would like to ask for advice.
What different is between Compile Time evaluation and RunTime evaluation in java? I think the "evaluation" means to determine of value of variable?
String str = "Hi" + "Bye"
Here, because there are literals values, these values are evaluation of compile time? What exactly does it mean? What JVM does after? JVM allocate memory on the heap?
String a = "Hi";
String result = a + "hello";
Evaluated on RunTime in JVM? JVM allocates the memory on the heap?
Thanks
Explain of compiletime evaluation and runtime evaluation
Just what the names say. Compile time evaluation is evaluation performed when the Java code is being compiled. Runtime evaluation is evaluation performed while the program is running.
Evaluation means to determine the value of an expression. A variable name by itself is an expression, but usually it is just one part of a larger expression. Also expressions are evaluated for more reasons than just to determine a value to assign to a variable.
Java classifies certain expressions as compile-time constant expressions. This is a subset of all expressions that conceivably could be evaluated at compile time. Certain contexts in Java source code require expressions of this kind to be used (the values of
caselabels, for example), and in those particular contexts, Java effectively requires them to be evaluated at compile time.Java also specifies that compile-time constant expressions of type
Stringare automatically interned. This means that every evaluation of that expression will return a reference to the sameStringobject, which effectively requires all compile-time constant expressions of typeStringto actually be evaluated at compile time. The same is not true of compile-time constant expressions of primitive type, but in practice, these ordinarily are computed at compile time, too, even when it is not technically required, as this is easy and safe to do and makes the resulting class files smaller and faster.There may be expressions that are not compile-time constant expressions in the JLS-defined sense, but that nevertheless can be computed at compile time (but note that "can be" is much more nuanced for reference types than for primitive types). The JLS does not forbid these being computed at compile time.
The expression
"Hi" + "Bye"is a compile-time constant expression as defined by the JLS. It is necessarily evaluated at compile time because of the interning requirement discussed above.The JVM is not involved in computing the string concatenation. It retrieves a reference to the interned
Stringobject representing the result, and stores that in variablestr.Like any other object, the one representing the concatenated string does live on the heap. Because an interned string is used, however, that object already exists. No new allocation is performed.
"Hi"and "hello" are compile-time constant expressions.aanda + "hello"are not, as the language spec defines it. Although the compiler could conceivably recognize that the initial value ofresultis always a string that comparesequals()to"Hihello", the JVM is nevertheless obligated to create a newStringobject for each evaluation ofa + "hello". That will live on the heap, like any other object. It may compareequals()to otherStringobjects live in the VM at the same time, but initially there will be no other references that compare==to the value ofresult.