Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion with String...

 
Suketu Patel
Greenhorn
Posts: 23
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi All,

I'm having a confusion with String concatenation and assigning... Please correct me if I'm not, I got that by concatenating two String reference variable or literal with "+" sign, the JVM automatically create new object for the same reference variable with updated value and an updated literal in other case.

My confusion with using concat() method... as per my knowledge String is immutable and can not be changed or updated after it first initialized. for e.g.

Confusion --- Confusion --- Confusion

If we change the above code with as follows will be result in abc12345


Does it work as same as I mentioned earlier that JVM create new object for the same reference variable with new updated values???

Thank,
S B Patel
 
Sam Knowski
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe I understand what you're asking.

Yes, strings are immutable. Unless the concatenated string is null, when you call the concat method you're getting a new String object.

Here's the concat method decompiled for your reference:
 
salvin francis
Bartender
Posts: 1339
11
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont quite understand your question, but let me explain a few things with an example:


As you can see, there are 3 references created, s,s1 and s2. You can re-assign the reference to another object, the actual object itself hasn't changed.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S B Patel wrote:Please correct me if I'm not, I got that by concatenating two String reference variable or literal with "+" sign, the JVM automatically create new object for the same reference variable with updated value and an updated literal in other case.

You are definitely incorrect! First of all, a String object is immutable, meaning once you have created a String object, its value can never be changed. Never! So when you make a change to a String object (e.g. concatenating another String using + operator or concat() method, changing case, replacing characters,...), a new String object will be created. If this new String object is not assigned to a String reference variable, the new String object is lost (and will be eligible for garbage collection). That's what happens when you manipulate an (immutable) String object! You should definitely read this excellent article about String literals and the String Literal Pool. And you must have a very good understanding of this topic, because String concatenation will appear in almost every exam question even when this question is testing your knowledge about e.g. switch statement or for loop. So if you struggle with this topic, you might fail the exam.

Now let's see how this works in practice using your code snippets. Let's start with the first oneOn line1 the String literal "abc" is assigned to reference variable s. On line2 the String literal "12" is concatenated to the String object referenced by reference variable s. And as you know by now a new String object is created (with value "abc12") and this newly created String object is assigned to reference variable s1. So reference variable s1 refers to a String object "abc12". And if you don't believe me, just add the statement System.out.println(s); to your code snippet and you'll see yourself it always prints "abc". On line3 the String literal "345" is concatenated to the String object referenced by reference variable s1. So again, a new String object is created (with value "abc12345"), but this newly created String object is not assigned to a reference variable and therefore is lost (and will be eligible for garbage collection). On line4 "abc12" (the String object referenced by reference variable s1) is printed.

Moving on to the second code snippetline1 and line2 is exactly the same as with the first code snippet. On line3 the String literal "345" is concatenated to the String object referenced by reference variable s1. So again, a new String object is created (with value "abc12345") and this time the newly created String object is assigned to reference variable s1. Because a new String object is assigned to reference variable s1, the previous String object s1 was referring to ("abc12"), is now lost (and will be eligible for garbage collection). On line4 "abc12345" (the String object referenced by reference variable s1) is printed.

Finally let's have a look at the String concatenation operator (+). Let's rewrite both code snippets using the + operator instead of the concat() methodBoth code snippets will behave exactly the same as the corresponding code snippet with the concat() method. In the second code snippet, you could also use the compound assignment operator on line3. So if you replace line3 in the second code snippet withnothing will change as both statements s1 = s1 + "345"; and s1 += "345"; are equivalent.

Hope it helps!
Kind regards,
Roel
 
Suketu Patel
Greenhorn
Posts: 23
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thank you for your reply... Specially thanks to Roel De Nijs for understanding my concern and your time to explain me in such a nice and easy way... You have solved my confusion on this topic Once again thank you very much.

Thanks,
S B Patel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic