0

My code is :

String str = "Hello";
String str1;
str1 = str;
str1 = str1 + " World";
System.out.println(str1);
System.out.println(str);

The output I get is :

Hello World
Hello

The output I was expecting is Hello World for both the cases because according to my understanding after str1 = str both objects are referencing to same location so if I change the content of one object other should also get affected.

So, is str1 = str1 + " World"; creating a new string object at different memory loction.?

7 Answers 7

7

Strings are immutable. When you do str1 = str1 + " World";, you're creating a totally new String and updating str1's reference to it. You're never re-asigning str so this IS the expected behaviour.

0
3

The point is that the + operator creates a new String object, effectively concatenating the operands. It does not modify the object str.

You might want to read http://javarevisited.blogspot.co.at/2010/10/why-string-is-immutable-in-java.html, which explains the concept of immutable strings quite nicely.

The class StringBuffer might also be worth noting, which is optimized for long, cascaded concatenations.

2
  • Interesting blog post. Thanks for it.
    – RanRag
    Sep 28, 2012 at 8:06
  • It was really the first google hit for java string immutable ;) Sep 28, 2012 at 8:07
2

In Java String is immutable.

str1 = str1 + " World"; It will create a new instance and assign to str1.

2

java.lang.String is immutable

str1 = str1 + " World";

This code will make str1 reference to a new created String object "Hello World", and str still reference to the "Hello" object.

2

Reference to primitive datetypes creates always a new Object

1

No, this is the right out put as in the code you had init the string

String str = "Hello";
 String str1;

Then you say

str1 = str;

it means str1 contains "Hello"

str1 = str1 + " World";

The above line will print "Hello World" because str1 having "Hello" and " World" is given there as a new string

and when you print str it is containing the word "Hello"

System.out.println(str);
0

What is so special about String in here, It's the common behavior for all. It's about the assignment operator (in line number 4 you assigning it again). Your code is similar to below.

Person p1 = new Person("Stack");
Person p2;
p2 = p1;
p1 = new Person("OverFlow");

What would this Print? Would both print "Overflow"?

From your words.

The output I was expecting is Hello World for both the cases because according to my understanding after str1 = str both objects are referencing to same location so if I change the content of one object other should also get affected.

Line 3: str1 = str;

As per Line number 3 you are correct.

Line 4: str1 = str1 + " World";

But in line number 4 you are not changing the content, you are reassigning str1 to a different object.

Here str, str1 and p1 are Object references. Not real objects. You would get same content if you are changing the object. Like

p1 = p1.setName("Overflow");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.