8

I know the difference between String literal and new String object and also know how it works internally. My question is a little bit advance of this. When we create String object using new keyword as

String str = new String("test");

In this case, we are passing a argument of String type. Where does this string get generated, heap or string constant pool or somewhere else?

As to my knowledge, this argument is a string literal, so it should be in String constant pool. If it is so, then what is use of the intern method, only just link variable str to constant pool? because "test" would be available already.

Please clarify me, if I had misunderstood the concept.

2 Answers 2

14

The statement String str = new String("test"); creates a string object which gets stored on the heap like any other object. The string literal "test" that is passed as an argument is stored in the string constant pool.

String#intern() checks if a string constant is already available in the string pool. If there is one already it returns it, else it creates a new one and stores it in the pool. See the Javadocs:

Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

Starting from JDK7, interned strings are stored on the heap. This is from the release notes of JDK7:

In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.

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

6 Comments

what intern method do - just linking?
@AnkitSharma - If a String is not already added to the String constants pool, calling intern() will add the String to the String constants pool.
But In this case String str = new String ("test") test would be available in constant pool every time.
@AnkitSharma Yes and the new String creates a new object each time it is called. The literal test is still the same in the pool.
@ArunRaaj It could be for example when name contains text read from a file or from user input. In that case, it's not present in the pool.
|
9

Use of intern() :

public static void main(String[] args) throws IOException {
    String s = new String(new char[] { 'a', 'b', 'c' }); // "abc" will not be added to String constants pool.
    System.out.println(System.identityHashCode(s));
    s = s.intern();// add s to String constants pool
    System.out.println(System.identityHashCode(s));

    String str1 = new String("hello");
    String str2 = "hello";
    String str3 = str1.intern();
    System.out.println(System.identityHashCode(str1));
    System.out.println(System.identityHashCode(str2));
    System.out.println(System.identityHashCode(str3));
}

O/P :

1414159026
1569228633 --> OOPs String moved to String constants pool

778966024
1021653256 
1021653256 --> "hello" already added to string pool. So intern does not add it again.

2 Comments

you are absolutely correct. But my confusion is - even you don't have hello in string constant pool and you typed String str = new String("hello") .So in this case literal hello gets generated in pool first then string object gets created on heap using this literal. It mean intern never add any literal in constant pool in above case.
@AnkitSharma - Yes. In this case intern() won't add "hello" to String consatnts pool

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.