• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String in VM pool?

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many Strings are in the VM string-pool after executing this code fragment ?
String s1 = "javacoding.net";
String s2 = "javacoding.net";
String s3 = new String("javacoding.net");
Answer given: 2
I think only 1 is created. Am I right?
Thanks.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
I think the answer given i.e 2 Strings are in the VM string-pool is correct.
Let's analyse
String s1 = "javacoding.net";
creates a new String object and places the String literal "javacoding.net" in the pool
String s2 = "javacoding.net";
refers to the same String literal created above. So no new String is created.
String s3 = new String("javacoding.net");
creates 2 objects(because we used the new keyword, Java will create a new String object in the normal (nonpool) memory and s3 will refer to it and the String literal "javacoding.net" is placed in the pool.
So at the end , 2 String literal's are created.
Hope it helps, Good luck,
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Look for the byte code instruction ldc (lines 0, 3, 10).
ldc <index> is the byte code instruction to load a reference to an instance of class String representing a String literal.
The same reference is loaded three times. The first two times, the reference is stored in local variables s1 and s2. The third time, the reference is passed to the String constructor. The String constructor will build an array of char using that reference.
So I would say 1 String object is interned and 1 String object is not. The interned String object is created from the String literal. Only 1 String object is created from the three String literals.
[ November 09, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS 3.10.5 gives a much better explanation,
String literals are "interned" so as to share unique instances.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the answer is 2.You can test it with "==";
code:
class Test {
public static void main(String s[]){
String s1="feoj";
String s2="feoj";
String s3=new String("feoj");
System.out.println(s1==s2);
System.out.println(s1==s3);
}
}
The result is: true
false
Let's analyse
(My english is very poor,I expect you can understand what i say)
Reference s1 and s2 will refer to the String "feoj" only at compile time.But reference s3 refers to String "feoj" at run time. So when s3 is created they are not refer to the same String; You can follow it up in the book "Thinking in Java".
If you still don't understand it.Send E-mail to me:miracle-5@163.com
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Yang,
There are two String objects. One is interned and one is not.
Some people and the String API talk about a mysterious string pool. This pool holds String objects that have been interned. I think the question Cathy posted is asking how many String objects are in that pool.
String literals are interned. String objects created with the String constructor are not, unless you explicitly intern them with the String.intern method.
[ November 09, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is an edited extract from the String class source code.

The source shows us that a String object represents a string of text as an array of char. The String object has a reference to an array of char.
When we say new String(�abc�), a reference to an existing String object is passed to the constructor. The constructor assigns that reference to a variable of type char[].

Both the interned String object created from the String literal �abc� and the new String object refer to the same array of char.
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marlene.
 
reply
    Bookmark Topic Watch Topic
  • New Topic