5

i know that when there is already an existing string in pool then a new string literal wont be made again.

i know the difference between string constant pool and heap also

i just want to know when is string pool created for a class for the below example.

String s="qwerty"; String s1="qwer"+"ty";// this will be resolved at compile time and no new string literal will be made String s2=s.subString(1); will create qwerty at run time s==s1; //true s==s2;//false 

i want to know for String s1 is resolved at compile time does that mean string pool is created at compile time ??

2 Answers 2

10

The constant pool contains String instances, which are runtime artifacts. Clearly, you cannot create objects before you start the program they are used in. The data specifying which string constants will be created is prepared at compile time and is a part of the class file format.

However, note that the string constants are created at class loading time, and not on either class initialization time or their first use. This is a point which people often confuse.

In your example, the difference is not between compile time and runtime, but between creating the string once in the constant pool, and creating it every time a line of code is executed.

Also do note that the string pool has been a part of the regular heap for a long time in OpenJDK (even before it has become OpenJDK).

Sign up to request clarification or add additional context in comments.

5 Comments

When we say "resolved at compile time", we mean: checking, if a variable of type String and with the name s1 has been declared already. This is checked by the compiler, but does not have anything to dowith constant pooling.
@LuigiEdlCarno OP here means that the concatenation expression is resolved as a compile-time constant expression, and will be treated as a string literal.
@Marko Topolnik Thanks for you answer and reminding me that string pool is a part of heap and i knew that objects are made at run time but i wanted to why is it said that concatenation of two string literals happen at compile time (String s1="qwer"+"ty"
@anubhav. Because the string literals are known to compiler. And also because it's specified in the specification.
@anubhav The key concept is that of compile-time constant expression.
2

As per your code :

String s2=s.subString(1); //this will create werty not qwerty so s==s2 will be anyways false 

If you use

String s2=s.subString(0); //this will create qwerty 

and then s==s2 will return true.

Also there is a method intern() which looks into constant pool for below case as well:

String s2 = new String("Qwerty").intern(); 

In this case, s==s2 will return true but if String s2==new String("Qwerty"); then s==s2 will return false.

Also string literal were part of permgen space before JDK 7 after which they became a part of heap space.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.