Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with String and StringXxx objects

 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitin Kumar
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one object on the heap....2 string literals which go the string pool
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Justin Fleming
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also see three objects created.
 
rushikesh sawant
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!! ?
 
Justin Fleming
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More ? how many in this example. if sb.append(" ") creates one more then String s1 = s1+" " also creates one. is it ?
 
Vinay Dinakar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic