在Java中设置相等:按值或引用? [英] Setting equal in Java: by value or reference?

查看:150
本文介绍了在Java中设置相等:按值或引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了两次测试,第一次以字符串开头

I did two tests, the first starting with Strings

    String str1 = "old";
    String str2 = str1;
    str1 = "new";

    System.out.println(str1); //new
    System.out.println(str2); //old

上面的例子表明 str2 = str1,按值

现在我进行类似的操作,但这次使用列表

Now I do the similar operations, but this time with Lists

    List<Integer> list1 = new ArrayList<Integer>();
    List<Integer> list2 = list1;
    list1.add(1);

    System.out.println(list1.size()); //1
    System.out.println(list2.size()); //1

此示例通过引用表示 list2 = list1

我很困惑,哪些Java变量/对象值,哪些是参考

I am confused, which Java variables/objects are passed by value and which are passed by reference?

推荐答案

在你的第一个代码中,是的,这一行

In your first code, yes, this line

String str2 = str1;

str2 分配给相同的字符串 str1 引用,即old。此时,它们是同一个对象。但是,下一行

Assigns str2 to the same String referred by str1, that is, "old". At this point, they are the same object. However, the next line

str1 = "new";

创建一个 字符串的实例,并将 str1 的引用更改为此新String。由于我们正在更改 str1 的引用,因此不会更改 str2 的内容。

create a new instance of String, and changes the reference of str1 to this new String. As we are changing the reference of str1, the content of str2 are not changed.

注意Java, String 是不可变的,即初始化后不能改变状态。以这种方式思考,的内容可能永远不会改变。因此,当您将new分配给 str1 时,您不会更改的值旧,你创建一个新的字符串

Pay attention that Java, Strings are immutable i.e. cannot change state once initialized. Thinking this way, content of "old" may never change. So when you assign "new" to str1, you don't change the value of "old", you create a new String instead.

换句话说,此行与此处相同

In other words, this line, in here, is the same as

str1 = new String("new");

http://i.minus.com/jboQoqCxApSELU.png

但是,在第二个代码中,

However, in the second code,

List<Integer> list2 = list1;

make list2 引用相同的列表 list1的。因此, list1 list2 引用相同的列表。那么

make list2 refer to the same list as list1. As a result, list1 and list2 refer to the same list. Then

list1.add(1); 

list1 。但是,正如我所说, list1 list2 引用相同的列表, list1 list2 现在有元素 1 。方法调用中没有创建新实例。

adds an element to the list referred by list1. However, as I have said, list1 and list2 refer to same list, both list1 and list2 now have the element 1. There is no new instance created in the method call.

http://i.minus.com/jxDLyBqcUzgHZ.png

事实上,如果你要做的话

In fact, if you were to do

List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = list1;
list1 = new ArrayList<Integer>();
list1.add(1);

System.out.println(list1.size()); //1
System.out.println(list2.size()); //0

因为 list1 = new ArrayList< Integer>(); list1 重新分配给新列表,该列表不再引用 list2 引用的对象。

because list1 = new ArrayList<Integer>(); reassigns list1 to a new list, that no longer refer to the object referred by list2.

毕竟赋值运算符(即 obj1 = obj2 总是复制引用,这两个引用在赋值后仍将引用同一个对象实例。这适用于 String List 任何其他类(但不是原始类型) )。

After all the assignment operator (i.e. obj1 = obj2) always copy the references, which two references will still refer to the same object instance after the assignment. This is for both String, List, or any other classes (But not primitive types).

但是, str1 =new在大多数情况下会创建一个<$的新实例c $ c> String 然后将对新 String 的引用分配给 str1 - 这是Java语言中的一个特例。这不适用于任何其他类型的对象。这与 list1.add(1)等任何其他方法调用不同。

However, str1 = "new" will, in most cases, create a new instance of String and then assign the reference to the new String to str1 - this is a special case in the Java lanaguage. This don't apply to any other kind of objects. This is different to any other method call like list1.add(1).

这篇关于在Java中设置相等:按值或引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆