-1

Scenario 1

public static void main(String[] args) throws Exception {
        String s1 = "Testing";
        String s2 = new String("Testing");
        s2.intern();
        if(s1==s2){
            System.out.println("s1 equals to s2");
        }else{
            System.out.println("s1 is not equal to s2");
        }

    }

OUTPUT: s1 is not equal to s2.

Scenario 2

public static void main(String[] args) throws Exception {       
        String s1 = "Testing";
        String s2 = new String("Testing").intern();
        if(s1==s2){
            System.out.println("s1 equals to s2");
        }else{
            System.out.println("s1 is not equal to s2");
        }
    }

OUTPUT: s1 equals to s2 .

My question is what is the difference between new String("Testing") and new String("Testing").intern() ?

Vishnu667
  • 768
  • 1
  • 16
  • 39

3 Answers3

3

Read the documentation for String.intern():

Returns a canonical representation for the string object.

Your code discards the return value, so there's no reason to expect any effect.

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • As an addendum, sometimes, code not assigning the result of `intern()` appears to work, because calling `intern()` on a string may have the side effect of this instance becoming the canonical instance. This doesn’t happen here, due to the previous assignment, `String s1 = "Testing";`, in other words, the canonical instance for the string `"Testing"` already exists and is referenced by `s1`, which is definitely distinct from the instance referenced by `s2`. So the latter instance can not become the canonical instance and `intern()` must return a different reference, i.e. the same held in `s1`. – Holger Jan 30 '17 at 11:46
  • Well, of course, even `new String("Testing")` is sufficient to ensure that the canonical instance and the newly constructed one are distinct. In theory, there could be a runtime that collects the canonical instance before the `intern()` call, if no other references exists, but no real JVM will do that in practice… – Holger Jan 30 '17 at 11:52
2

Java String is immutable, so you must update the s2 reference.

Change s2.intern(); to

s2 = s2.intern();

And it will work the same way.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    Actually, @CKing, the key point to the answer is updating the reference. The immutability of `String` is a complete red herring - even if `String` were mutable, OP would have had the same issue. The first snippet fails because OP didn't assign the interned reference back to `s2`. I feel that Elliott made that particular point more clearly than you did. But to tell the truth, at the time I upvoted this answer, yours hadn't yet appeared on my screen. – Dawood ibn Kareem Jan 29 '17 at 07:33
  • @DavidWallace I did say in my answer that *you need to reassign any modifications* and elaborated with what I mean by that. I get it, this answer is better I don't deny that. But is it that much better that my answer stays at 0? I generally see this happen a lot here. Most of my accepted answers have fewer votes than the answers that were not accepted on the same quesiton :). Higher rep demands more votes here. That's just my perception. I don't expect anyone to agree. Anyway this is topic better suited for *meta* and is already discussed there so I am off. Have a good day. – Chetan Kinger Jan 29 '17 at 07:37
1

Read the Javadoc of intern() :

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.

Since String s1 = "Testing"; adds the String referenced by s1 to the String pool, new String("Testing").intern() returns an instance from the String pool equal to the String created by new String("Testing"), which is the same String instance referenced by s1.

On the other hand, in the first snippet, s2.intern() has no effect. The String equal to "Testing" is already in the String pool, so the String referenced by s2 is not added to the pool. And you are ignoring the return value of that method, which is the String referenced by s1. Therefore s1 and s2 refer to different objects.

Eran
  • 387,369
  • 54
  • 702
  • 768