Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String concatenation

 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

What about string concatenation by using the '+' operater. Does it creates new string objects?.

Example:
String str1 = 'frist name'
str1 = 'frist name' + 'last name'

My qustion is what is the number of objects after concatenation?
Similarly if we use replace() method on certain string object then whether it will create new string object or it will modify the existing object?

Thanks

Naveed
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String object are immutable , it means they can't be modified so when you replace any character in the string or concate it with another string , everytime you will get a new String object .

String s1 = "A";
String s2 = "A"+"B";

Now in total you have 3 objects : "A" , "B" and "AB" And one more thing in addition "B" object is eligible for Garbage Collection ...
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one more thing in addition "B" object is eligible for Garbage Collection ...


Thanks for pointing this out rathi.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String s1 = "A";
String s2 = "A"+"B";

Now in total you have 3 objects : "A" , "B" and "AB" And one more thing in addition "B" object is eligible for Garbage Collection ...

This is false.
You have 2 objects. One is an instance that has the value "A", the other is an instance that has the value "AB". "B" is not eligible for garbage collection because it is a String literal, not an object, and the same can be said for "A" and "AB".

The relevant part of the JLS is 15.28 where constant expression is defined.
"A" + "B" is a constant expression and is inlined at compile-time.
 
Aruna Agrawal
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:

"B" is not eligible for garbage collection because it is a String literal, not an object, and the same can be said for "A" and "AB".



u mean that "A" and "AB" are not objects but just literals. Pls explain
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"A" and "B" are both literals. For "A", there's also a String object in memory (at runtime) with length 1, containing the char 'A'. For "B", there could have been an object in memory, but since this is never needed at runtime, the compiler chose to omit it from the class file. "A" + "B" is a compile-time constant, which means that at runtime there is also a String object with length 2, contents 'A' and 'B'. Most of the time, we refer to a String object using either the literal or the variable name that refers to it. (E.g. "the String "A" or "the String myStr".) However in some discussions (such as this one) that's not sufficient, and we need to distinguish between an object and the things that refer to it. It's like names - on one level, I can say that I am Jim. But "Jim" is a name. I am not a name. Most of the time I don't need to make this distinction when talking to other people, but sometimes I do. Similarly, sometimes we need to distinguish between objects and the literals and variables that refer to them.
[ March 08, 2005: Message edited by: Jim Yingst ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic