• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

+ Operator in String

 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does + operator in string does not create a new String?
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where have you heard that it does not? Or are you asking why *does* it create a new string?

Henry
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you do

String str = "abc";
str = str + "be";

then the JVM creates string "abc" and then when you do the second operation, it creates a second string "abcbe" and assigns to the reference str.

If now you do String x = "abcbe"
then x refers to the same object that str refers above ( after the 2nd assignment).

But if you do String x = new String("abcbe");
then x refers to another object altogether.
 
Ranch Hand
Posts: 206
Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it does create a new String. Strings in Java are immutable. When you perform a Concatenation Operation, the resultant object is thus a new object.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well it does create a new String.


There is a small exception from this rule, as far as I know. The String class holds a pool of String references and it could be that you get a reference to an already created String. You can manipulate the defaut behaviour by using the String.intern() method. Therefore it is possible that Strings which pass the equals() test do also pass the '==' test. However, this is nothing to rely on and in general you can say that you'll get a new String object in most cases.

Regards,
Thomas
 
We noticed he had no friends. So we gave him this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic