| Author |
Problem with String and StringXxx objects
|
rushikesh sawant
Ranch Hand
Joined: Dec 22, 2009
Posts: 65
|
|
how many objects are created in this code fragment:
i think that there are 3 objects are created here. please tell is it right? i'm not sure about this one. Thanks.
|
SCJP 5.0 100%
|
 |
Nitin Kumar
Greenhorn
Joined: Apr 18, 2005
Posts: 29
|
|
Only one object is created as StrigBuffer is MUTABLE and one can change its state through functions.
Note : Strings are immutable and everytime you change the String "value" you create a new object.
Regards,
Nitin
|
 |
Raju Champaklal
Ranch Hand
Joined: Dec 10, 2009
Posts: 521
|
|
one object on the heap....2 string literals which go the string pool
|
scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Raju Champaklal wrote:one object on the heap....2 string literals which go the string pool 
Don't the string literals also go on the heap? There is no way to reach those two String objects, but wouldn't the answer still be 3 objects
BTW, Raju, congrats for getting SCJP!
|
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
|
 |
Justin Fleming
Greenhorn
Joined: Dec 30, 2009
Posts: 10
|
|
|
I also see three objects created.
|
 |
rushikesh sawant
Ranch Hand
Joined: Dec 22, 2009
Posts: 65
|
|
yea got it 3 objects are created.
1 goes on heap and 2 string literal objects goes in string constant pool.
and raju meant the same thing, 3 objects.
Thanks!
|
 |
Vinay Dinakar
Greenhorn
Joined: Dec 30, 2008
Posts: 17
|
|
|
Arent these string literal also objects ? then how 3 objects gets created, it should be only one right. if it is 3 objects, then whats the difference between string and stringbuffer !!! ?
|
Thanks,
~ Vinay Ds
|
 |
Justin Fleming
Greenhorn
Joined: Dec 30, 2009
Posts: 10
|
|
Vinay Dinakar wrote:Arent these string literal also objects ? then how 3 objects gets created, it should be only one right. if it is 3 objects, then whats the difference between string and stringbuffer !!! ?
If you were using String instead of String Buffer then even more objects would be created. This isn't the best example of a StringBuffer or evena StringBuilder in use though. Repeat the append about 20 times and it becomes more apparent.
One thing I wonder though, when you append a non string literal to a StringBuffer / Builder and it appends the string representation, does it still create a String literal that gets added to the pool? I would imagine it would but I don't know the answer.
|
 |
Vinay Dinakar
Greenhorn
Joined: Dec 30, 2008
Posts: 17
|
|
|
More ? how many in this example. if sb.append(" ") creates one more then String s1 = s1+" " also creates one. is it ?
|
 |
Vinay Dinakar
Greenhorn
Joined: Dec 30, 2008
Posts: 17
|
|
what i can see from this example
is that, line1 creates one object in heap ("abc") and in line 2, "def" goes to string constant pool and sb (line 1 's object) value gets modified. so total 2 objects. line 3 does not have any effect.
|
 |
Justin Fleming
Greenhorn
Joined: Dec 30, 2009
Posts: 10
|
|
Vinay Dinakar wrote:what i can see from this example
is that, line1 creates one object in heap ("abc") and in line 2, "def" goes to string constant pool and sb (line 1 's object) value gets modified. so total 2 objects. line 3 does not have any effect.
My understanding is that "abc" creates a String Literal (Obj1) , and it's value is transfered to the StringBuffer (object 2) , "def" creates a 3rd object (another String Literal)(Obj3), but is appended to the sb object rather than creating a new "abcdef" String. Line 3 does not create a new object.
So thats 3 objects. If the code was
Then we would have 4 String objects. The String sb is a String reference (OBJ1), "abc" is a literal that is copied to the reference(OBJ2). When we create a 3rd string "def"(OBJ3) and a 4th string "abcdef"(OBJ4) which is assigned to the String Reference variable sb. In this example you would have 4 object and 3 String Literals in the String Constant Pool.
Someone feel free to jump in if I am not correct.
|
 |
 |
|
|
subject: Problem with String and StringXxx objects
|
|
|