aspose file tools*
The moose likes Beginning Java and the fly likes String s1= Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "String s1="abc" & String s2= new String("abc")" Watch "String s1="abc" & String s2= new String("abc")" New topic
Author

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

Max White
Ranch Hand

Joined: Jun 28, 2008
Posts: 85
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
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

Joined: Jun 28, 2008
Posts: 85
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Jun 28, 2008
Posts: 85
Thanks,i got your point.But what about the object created because of the new operator in the 2nd statement?
Billy Tsai
Ranch Hand

Joined: May 23, 2003
Posts: 1304
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 ]

BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
Oracle Certified Master Java EE 5 Enterprise Architect
 
wood burning stoves
 
subject: String s1="abc" & String s2= new String("abc")