2

Can string pool contain two strings with the same value??

String str = "abc";
String str1 = new String("abc");

   Will the second statement with `new()` operator creates two objects of `string` "abc", one on `heap` and another on `string` pool? 

   Now if i call intern() on str1 ie str1.intern(); as a third statement, will str1 refer to the "abc" from String pool? 

  If yes then what will happen to the object that was created on heap earlier by the new(). Will that object be eligible for garbage collection.?
  If no then what will be the result of str1.intern();?
Yogesh Pangam
  • 39
  • 1
  • 5

5 Answers5

7

No first will also create one object and also second one will create only one string object. Difference is that first one will create in String pool and second one will create in heap only. If you will call str1.intern(); then it will be added to String pool.

String str1 = "abc";
String str2 = new String("abc");
Stirng str3 = "abc"

Here two objects will be created. first line will create one strong object with reference str1 and 3rd line will point to the same object created in 1st line with reference str3 but in 2nd line one new object will be created as we are using new keyword here. Hope it will help you.

Also check this answer. Good explanation is there.

Community
  • 1
  • 1
Android Killer
  • 18,174
  • 13
  • 67
  • 90
  • So you mean to say if i do the statements in reverse order, still there will be two objects? – Juned Ahsan Jul 31 '13 at 10:29
  • 2
    @JunedAhsan in whatever you do there will be two objects only. and i edited little bit my answer. Plz read it again. – Android Killer Jul 31 '13 at 10:31
  • No this is wrong. If you create an object even with new, it will still be part of the string pool. If it is followed by a simple string ="sameStirng" statement, the pooled object will be used and not a new one is created. – Juned Ahsan Jul 31 '13 at 10:33
  • 4
    no if you will use new in any situation a new object will b created even if there is another string with the same value present in string pool. going to edit the answer a little. – Android Killer Jul 31 '13 at 10:35
  • Who DV me please explain!!! – Android Killer Jul 31 '13 at 10:37
  • String str = "abc"; String str1 = new String("abc"); Now if i call intern() on str1 ie str1.intern(); as a third statement, str1 will refer to the "abc" from String pool. Then what will happen to the object that was created on heap earlier by the new(). Will that object be eligible for garbage collection.???? – Yogesh Pangam Jul 31 '13 at 10:52
  • Yes most probably as no live reference for it. But i m also not clear that much. – Android Killer Jul 31 '13 at 10:56
  • Correction: the execution of the statement `String str = "abc";` creates **zero** objects. All string literals already exist in the constant pool. – Marko Topolnik Jul 31 '13 at 11:08
1

"abc" object will be created on class loading and put on String pool. The second line will use String(String original) constructor where original is the pointer to "abc" in pool. This is the bytecode for the second line:

NEW java/lang/String
DUP
LDC "abc"
INVOKESPECIAL java/lang/String.<init>(Ljava/lang/String;)V
ASTORE 2
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
1

new String("abc") is a class instance creation expression, and must create a new object. Whether it shares the same char array internally as the literal "abc" is up to the String implementation. Both the "abc" references will use the same intern String.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • The docs say "the newly created string is a copy of the argument string". That sounds like no `char[]` sharing should be allowed to occur (even though not 100% explicit). – Marko Topolnik Jul 31 '13 at 11:12
  • @MarkoTopolnik The use of a char[] is not mentioned or required in the docs, so the "copy of" comment is unlikely to apply to it, rather than the String argument. In practice, the JDK 1.7 implementation makes a copy of the array if, and only if, the String argument is shorter than its array. That can happen as a result of substring operations. – Patricia Shanahan Jul 31 '13 at 15:50
  • Yes, that's why I say it's not 100% explicit, but note this: it doesn't say "a newly created `String`", but "a newly created string". And the actual string is stored in the `char[]`. – Marko Topolnik Jul 31 '13 at 15:57
1
    String t1 = "ABC";
    String t2 = new String("ABC");
    String t3 = "ABC";

    System.out.println(t1 == t2);
    System.out.println(t2 == t3);
    System.out.println(t1 == t3);

generates the following output (Java SE 7):

    false
    false
    true
AKDADEVIL
  • 206
  • 1
  • 7
0

It is basically :

// "abc" is interned by the JVM in the pool
String str="abc"; 
// JVM uses the pooled "abc" to create a new instance
String str1=new String("abc"); 

Only one new String object is created referenced by str1 . The String literal "abc" was interned by the JVM. Since the literal "abc" already existed in the String pool , in the second statement , JVM should use that .

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • 1
    `// JVM uses the pooled "abc" to create a new instance` - JVM first check the literal in pool and if present then using that JVM creates string in heap? – exexzian Jul 31 '13 at 10:30
  • @sansix How does that statement differ from mine ? – AllTooSir Jul 31 '13 at 10:31
  • i didnt downvoted you - I just have doubt - `JVM should use` that literal pool value or `uses` that value – exexzian Jul 31 '13 at 10:32
  • @sansix It should use as per the specs . – AllTooSir Jul 31 '13 at 10:33
  • 1
    @TheNewIdiot hey i wanted to know all these. How u knew sureshatta downvoted u ? is thr way to know who downvoted u ? if yes any other features like this that we can know ? – Android Killer Jul 31 '13 at 10:45
  • 2
    String str = "abc"; String str1 = new String("abc"); Now if i call intern() on str1 ie str1.intern(); as a third statement, it will refer to the "abc" from String pool. Then what will happen to the object that was created on heap earlier by the new(). Will that object be eligible for garbage collection.???? – Yogesh Pangam Jul 31 '13 at 10:51
  • @YogeshPangam you came up with a good point – exexzian Jul 31 '13 at 10:58
  • @AndroidKiller He doesn't know who down voted him, he just thinks he does. – Andrew Barber Aug 01 '13 at 18:40