• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String s1="abc" & String s2= new String("abc")

 
Max White
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having read this question in one of the post here,i tried looking for the solution myself and referred K&B & Effective Java & came to a conclusion but i m still not sure.
What i understood is :

class A {
.
.
String s1="abc";
String s2=new String "abc";
.
.
}

In the first statement one object & one reference(s1) will be created(assuming that the string pool is empty)and the string literal "abc" is placed in the pool.
Now the second statement also creates 1 object(technically 2) because of the new operator,1 reference(s2) and places the literal "abc" again in the pool only.
So finally we have 2 objects(1 in the pool and 1 in the heap),2 references(s1 & s2) and 2 same string literals "abc" in the string pool.
Am i correct?
Thanks for the reply.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well... no, not exactly, although a lot of this is on the right track.

The important thing about the "string pool" is that it's a set of unique strings. No string can be in the pool twice. If "abc" is in the pool, and you try to add it again, nothing happens. The "intern()" method says "try to put this in the pool, then either return this object (if it wasn't in the pool already) or any string equal to this one that was already in the pool."

The first line creates (sort of, see below) the String "abc" in the String pool, and s1 points to it. The second line creates a new String object, also containing the three characters "abc", and that's just a plain old heap object. The literal "abc" in the second line is the same object as the literal "abc" in the first line; the String constructor actually copies the literal passed to it, so after line 2 you've got two String objects, one in the String pool, and one in the plain old heap.

Now, I said "see below" regarding the creating of the String literals. The String is not actually constructed at line 1. It's constructed and put into the String pool when the class is loaded. All that line 1 does is fetch a reference to the String that's already in the pool, and store it in s1. The distinction really isn't that important until you check the bytecode with a Java disassembler like javap, at which point you'll see that there's a big difference.

Finally, note that you don't "create" references, really. They're not objects; they're just there. It might be useful to worry about counting the references to a given object sometimes, but not so much counting how many references there are to whatever in total.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nearly . . . When the JVM finds the String literal "abc" is already in the pool, it uses the same String object again.
But you are correct that "new" creates a 2nd distinct String object.

And welcome to JavaRanch
[ July 03, 2008: Message edited by: Campbell Ritchie ]
 
Max White
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Hill & Campbell.
What if i change the code a bit..


First of all is it valid? If it is(that's what i think), then in this case
what option does JVM has after the first statement?It has to create an object,that's for sure.But what about the reference?As far as i know,reference(s) will refer to the second object(created on the heap) and the first object(in the pool) will be isolated(& thus collected by GC).So finally we are left with 1 object(on the heap) & 1 reference(s).Is it
right?Thanks..
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings in the String pool do not get garbage collected, unless and until, possibly, the class that creates them is collected; this won't happen unless the CLassLoader that loads the class is collected. Normally, this won't happen, so it's not useful to talk about Strings in the String pool being collected.

Again, the literal "abc" refers to a particular String object in the String pool. If you refer to the same object twice in the same class, it's clear that in between those two points, the object can't possibly be garbage collected, yes?

And of course I already explained that the pool object is actually created long before this code even runs, when the class is loaded, so again, the string couldn't possibly be collected and somehow "reappear".
 
Max White
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,i got your point.But what about the object created because of the new operator in the 2nd statement?
 
Billy Tsai
Ranch Hand
Posts: 1304
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String class is designed with Flyweight design pattern. The String pool is stored in a memory area called perm which sits next to the heap, things in the perm will be always these, so the second object created with the String constructor will be stored in the heap but the first one will be stored in the String pool that is located in the perm
[ September 20, 2008: Message edited by: Billy Tsai ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic