简体   繁体   English

Java:字符串引用得到意外的输出

[英]Java: String reference getting unexpected output

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. 在两种情况下,我期望的输出都是Hello World ,因为根据我对str1 = str理解,两个对象都引用相同的位置,因此,如果我更改一个对象的内容,其他对象也将受到影响。

So, is str1 = str1 + " World"; 因此,是str1 = str1 + " World"; creating a new string object at different memory loction.? 在不同的内存位置创建一个新的字符串对象。

Strings are immutable. 字符串是不可变的。 When you do str1 = str1 + " World"; 当您执行str1 = str1 + " World"; , you're creating a totally new String and updating str1 's reference to it. ,您将创建一个全新的String并更新str1对它的引用。 You're never re-asigning str so this IS the expected behaviour. 您永远不会重新分配str因此这是预期的行为。

The point is that the + operator creates a new String object, effectively concatenating the operands. 关键是+运算符会创建一个新的String对象,从而有效地将操作数串联在一起。 It does not modify the object str . 它不会修改对象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. 您可能想阅读http://javarevisited.blogspot.co.at/2010/10/why-string-is-immutable-in-java.html ,它很好地解释了不可变字符串的概念。

The class StringBuffer might also be worth noting, which is optimized for long, cascaded concatenations. StringBuffer可能也值得注意,该类针对长而级联的连接进行了优化。

In Java String is immutable. 在Java中,String是不可变的。

str1 = str1 + " World"; It will create a new instance and assign to str1. 它将创建一个新实例并分配给str1。

java.lang.String is immutable java.lang.String是不可变的

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. 此代码将str1引用一个新创建的String对象"Hello World" ,而str仍然引用"Hello"对象。

引用原始日期类型总是创建一个新的对象

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包含“ Hello”

str1 = str1 + " World";

The above line will print "Hello World" because str1 having "Hello" and " World" is given there as a new string 上面的行将打印“ Hello World”,因为具有“ Hello”和“ World”的str1作为新字符串被给出

and when you print str it is containing the word "Hello" 当您打印str ,其中包含单词“ Hello”

System.out.println(str);

What is so special about String in here, It's the common behavior for all. 这里的String有什么特别之处,这是所有人的共同行为。 It's about the assignment operator (in line number 4 you assigning it again). 关于赋值运算符(在第4行中,您再次对其进行赋值)。 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. 在两种情况下,我期望的输出都是Hello World,因为根据我对str1 = str的理解, 两个对象都引用相同的位置,因此,如果我更改一个对象的内容,其他对象也将受到影响。

Line 3: str1 = str;

As per Line number 3 you are correct. 根据第3行,您是正确的。

Line 4: str1 = str1 + " World";

But in line number 4 you are not changing the content, you are reassigning str1 to a different object. 但是在第4行中,您没有更改内容,而是将str1重新分配给了另一个对象。

Here str, str1 and p1 are Object references. 在这里,str,str1和p1是对象引用。 Not real objects. 不是真实的对象。 You would get same content if you are changing the object. 如果更改对象,您将获得相同的内容。 Like 喜欢

p1 = p1.setName("Overflow"); p1 = p1.setName(“ Overflow”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM