• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String concatenation

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...
 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And one more thing in addition "B" object is eligible for Garbage Collection ...



Thanks for pointing this out rathi.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
We've gotta get close enough to that helmet to pull the choke on it's engine and flood his mind! Or, we could just read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic