为什么创建一个数组从原语复制呢? [英] Why does creating an array from primitives copy them?

查看:210
本文介绍了为什么创建一个数组从原语复制呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在节目

 公共静态无效的主要(字串[] args){
    INT X = 1;
    INT Y = 2;
    INT [] Z = INT新[] {X,Y};
    Z [0] = 3;
    的System.out.println(X); //输出1
}

我预计3将被打印出来。但1了。为什么?我认为Java使得当我们通过一个refrence的方法,该方法将与另一个参照同一对象进行操作仅复制引用,即。这就像C ++的指针和基本类型。

于是,我就咨询 JLS 10.6 并没有发现任何关于它有用。也许我已经得到了有关原始类型Java中的一些误解。你能否澄清?


解决方案

  

为什么创建一个数组从原语将它们复制?


有关完全相同的原因:

  int类型的= 5;
INT B = A;

...副本 A B ,而不会产生任何环节之间的<$ c的值$ C> A 和 b :本的的被复制,而不是某种参考变量


回复您的评论:


  

但是,当我们在引用类型,参考副本运行,不是吗?


是的,确实如此,酷似号被复制在 INT 场景。

B = A (或阵列初始化)的作品完全相同的方式无论 A b 是原始类型变量或引用类型变量:本的的在 A 复制 b ,再有就是之间没有任何正在进行的链接 A b

唯一的区别是,当你正在处理的引用类型,在变量的值不是实际的东西,它是一个的参考的实际的东西。因此,复制从一个变量引用到另一个只是使使这两个变量指向同一件事,就像我的code以上的品牌 A 都有值5。

考虑:让我们创建一个列表:

 列表&LT;串GT;一个=新的ArrayList&LT;串GT;();

这为我们提供了这样的事情在内存中:


              + ---------- +
A(55465)-----> |名单|
              + ---------- +

变量 A 包含的值是一个对象的引用。我上面描述为55465的价值,但我们从来没有真正看到我们的Java code中的原始值(和它改变了垃圾收集完成)。这只是一个值,就像任何其他的价值,它告诉JVM在哪里可以找到在内存中的对象。你可以认为它像一个包含存储器地址(这不是它到底是什么,但它的作品在概念上)。

现在我们这样做:

 列表&LT;串GT; B = A;

现在我们已经在内存中是这样的:

A(55465) -  +
          | + ---------- +
          + ---> |名单|
          | + ---------- +
B(55465) - +

两者 A B 包含的的指到列表中。

您可以看到这是完全一样:

  int类型的= 5

给我们


一个(5)

然后

  INT B = A;

给我们


一个(5)
B(5)

的复制变量之间,传递给函数等引用类型唯一的区别是什么,价值的用途,它是如何PTED间$ P $。


  

如果是这样,我会说,Java是由价值复制原语的值类型和引用类型的引用感通。对吧?


没有。 按值传递和按引用传递在计算一个特定的含义:这是关于有一个的变量的,而不是一个对象的引用。这就是传递按引用如下:

  //不是Java; Java没有这
无效美孚(INT&安培; A){//假传通过引用事
    一个= 3;
}INT 1 = 5;
富(安培; A); //假传通过引用运营商
的System.out.println(一); // 3

传递通过引用无关与对象引用。它们共有的唯一的事就是两个字参考。 Java是一种纯粹的传递按值语言。

In the program

public static void main(String[] args){
    int x = 1;
    int y = 2;
    int[] z = new int[]{x, y};
    z[0] = 3;
    System.out.println(x); //prints 1
}

I expected that 3 would be printed. But 1 was. Why? I thought Java makes copy only of references, i.e. when we pass a refrence to a method, the method will operate with another reference to the same object. It's like C++ pointers and primitive types.

So I tried to consult JLS 10.6 and didn't find anything useful about it. Maybe I've got some misunderstanding about primitive types in Java. Could you clarify?

解决方案

Why does creating an array from primitives copy them?

For exactly the same reason that:

int a = 5;
int b = a;

...copies the value of a into b, without creating any kind of link between a and b: The value is copied, not some kind of reference to the variable.


Re your comment:

But when we operate on reference types, the reference copies, doesn't it?

Yes, it does, exactly like the number gets copied in your int scenario.

b = a (or your array initializer) works exactly the same way regardless of whether a and b are primitive type variables or reference type variables: The value in a is copied to b, and then there is no ongoing link between a and b.

The only difference is that when you're dealing with reference types, the value in the variable isn't the actual thing, it's a reference to the actual thing. So copying that reference from one variable into another just makes makes those two variables refer to the same thing, just like my code above makes a and b both have the value 5.

Consider: Let's create a list:

List<String> a = new ArrayList<String>();

That gives us something like this in memory:

              +----------+
a(55465)----->| the list |
              +----------+

The variable a contains a value which is a reference to an object. I've depicted that value above as 55465, but we never actually see the raw value in our Java code (and it changes as garbage collection is done). It's just a value, like any other value, which tells the JVM where to find the object in memory. You can think of it like a long containing a memory address (that's not what it really is, but it works conceptually).

Now we do this:

List<String> b = a;

Now we have something like this in memory:


a(55465)--+
          |    +----------+
          +--->| the list |
          |    +----------+
b(55465)--+

Both a and b contain a value which refers to the list.

You can see how that's exactly like:

int a = 5

gives us

a(5)

and then

int b = a;

gives us

a(5)
b(5)

Values are copied between variables, passed into functions, etc. The only difference with reference types is what that value is used for, how it's interpreted.

If so, I would say that java is pass by value in the sense of copying values of primitives and the references of reference types. Right?

No. "Pass by value" and "pass by reference" have a specific meaning in computing: It's about having a reference to a variable, not an object. This is what pass-by-reference looks like:

// NOT JAVA; Java doesn't have this
void foo(int &a) { // Fake pass-by-reference thing
    a = 3;
}

int a = 5;
foo(&a); // Fake pass-by-reference operator
System.out.println(a); // 3

Pass-by-reference has nothing to do with object references. The only thing they have in common is the word "reference." Java is a purely pass-by-value language.

这篇关于为什么创建一个数组从原语复制呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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