Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to know about String, String Constant pool and String intern method [duplicate]

  1. Say if there are no strings in the String constant pool, and if I say,

    String s = "Java";
    

    Then how many objects will be created?

  2. Now again nothing in the pool, and I say,

    String s = new String("Java");
    

    Now, how many objects will be created?

  3. Now again nothing in the pool, and I say,

    String s = new String("Java");
    s.intern();
    

    What will the intern method do?

  4. Now again nothing in the pool, and I say,

    String s = new String("Java");
    String s1 = s.intern();
    

    What will happen now?

Please answer as I am really confused about it.

As I read in SCJP5 Kathy Sierra book, that when you create a String with new, then 2 objects are created, one on the heap and one in the pool.

like image 935
RohitT Avatar asked Mar 06 '23 18:03

RohitT


2 Answers

I will assume that in each example below you load and execute the code exactly once, in a new JVM each time. (I will also assume that nowhere else in your code do you use the literal "Java" ... since that would complicate things.)


1) Say if there are no strings in the String constant pool, and if i say,

String s = "Java";

Then how many objects will be created ?

One string is created and added to the pool when method is loaded.


2) Now again nothing in the pool, and i say,

String s = new String("Java");

Now how many objects will be created.

One string is created and added to the pool when method is loaded.

A second string is created by the new when the code is run, and it is NOT added to the pool.


3) Now again nothing in the pool, and i say,

String s = new String("Java");
s.intern();

What will the intern method do ?

One string is created and added to the pool when method is loaded.

A second string is created by the new, and it is NOT added to the pool.

The intern call returns the first string. (You don't keep the reference ...)


4) Now again nothing in the pool, and i say,

String s = new String("Java");
String s1 = s.intern();

What will happen now?

Same as example 3. Thus, s1 will hold a reference to the String object that represents the "Java" string literal.


I read in SCJP5 Kathy Sierra book, that when you create a String with new, then 2 objects are created, one on the heap and one in the pool.

I doubt that the book said that exactly. (You are paraphrasing, and I think you have paraphrased somewhat inaccurately.)

However, your paraphrasing is roughly correct, though (an this is important!) the string object representing the literal is created and added to the pool when the code fragment is loaded1, not when it is executed.


And to address another point of confusion:

"What i actually meant was that from the answer that you gave, it seems that a String will always be added in the String constant pool."

That is incorrect. It is a false generalization.

While it is true for all 4 of the cases above, it will not be true for others. It depends on where the original string came from. In typical applications, most text data is read from a file, socket, or a user interface. When that happens, the strings are created from arrays of characters, either directly or via a library call.

Here is a simple (but unrealistic) example that shows creating a String from its component characters.

String s = new String(new char[]{'J', 'a', 'v', 'a'});

In the snippet above, only one String is created, and it is NOT in the String pool. If you wanted the resulting string to be in the string pool you need to explicitly call intern something like this:

String s = new String(new char[]{'J', 'a', 'v', 'a'});
s = s.intern();

... which will (if necessary) create a second string in the string pool2.


1 - Apparently, in some JVMs creation and interning string literals is done lazily, so it is not possible to say with 100% certainty when it actually happens. However, it will only occur once (per class that references the literal), no matter how many times the code fragment is executed by the JVM.

2 - There is no way to new a string into the string pool. It would actually be a violation of the JLS. The new operation is specified by the JLS as always creating a new object.

like image 178
Stephen C Avatar answered Mar 11 '23 22:03

Stephen C


  1. Then how many objects will be created?

There is one String in the pool.

  1. Now how many objects will be created?

One String will be created, there is still one String in the pool.

  1. What will the intern method do?

It will try to put a "Java" into the pool, find another "Java" there, are return a reference to that "Java" from step 1.

  1. What will happen now?

The "Java" from step 1 will come back and s1 now refers to it.

like image 25
Andrew Tobilko Avatar answered Mar 11 '23 21:03

Andrew Tobilko