This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String concatenation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String concatenation" Watch "String concatenation" New topic
Author

String concatenation

Naveed Ali
Ranch Hand

Joined: Aug 30, 2002
Posts: 41
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

Joined: Oct 11, 2004
Posts: 3830
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

Joined: Sep 28, 2004
Posts: 1228
And one more thing in addition "B" object is eligible for Garbage Collection ...


Thanks for pointing this out rathi.


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

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.


Tony Morris
Java Q&A (FAQ, Trivia)
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
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

Joined: Jan 30, 2000
Posts: 18671
"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 ]

"I'm not back." - Bill Harding, Twister
 
wood burning stoves
 
subject: String concatenation
 
Similar Threads
string concatenation
String and StringBuffer
Can anyone explain what is == and equal( ) ?
equals versus ==
String class