small clarification in String literals

Hi

System.out.print((hello == ("Hel"+"lo")) + " "); Returns "TRUE"

Reason:Strings computed by constant expressions compile time and then treated as if they were literals.

Is it true?String Literal memory allocation at compile time or runtime?

Thanks

**smile

[294 byte] By [smile4ua] at [2007-11-27 11:40:45]
# 1

See the Java Language Specification:

http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5

Niceguy1a at 2007-7-29 17:33:41 > top of Java-index,Java Essentials,Java Programming...
# 2

> Is it true?String Literal memory allocation at

> compile time or runtime?

Memory allocation always happens at runtime, since nothing is executed during compile time.

But yes, true. "Hel"+"lo" in this case gets compiled to "Hello" which already exists. Works differently if any part of the concatenation is a variable.

CeciNEstPasUnProgrammeura at 2007-7-29 17:33:41 > top of Java-index,Java Essentials,Java Programming...
# 3

>

> Is it true?String Literal memory allocation at

> compile time or runtime?

>

String data is stored in the constants table of the class, but when they are loaded the actual, coresponding, String objects are stored in the "intern" pool (after being checked for uniqueness).

This is allocated at run time, and you can add Strings to it explicitly with String.intern()

malcolmmca at 2007-7-29 17:33:41 > top of Java-index,Java Essentials,Java Programming...