更改方法中变量的值,Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/489913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:17:42  来源:igfitidea点击:

Changing the values of variables in methods, Java

javascope

提问by user42155

I have a question about changing the values of variables in methods in Java.

我有一个关于在 Java 中更改方法中变量值的问题。

This is my code:

这是我的代码:

public class Test {
    public static void funk(int a, int[] b) { 
        b[0] = b[0] * 2; 
        a = b[0] + 5;
    } 

    public static void main(String[] args) {
        int bird = 10;
        int[] tiger = {7};

        Test.funk(bird, tiger);
    }
}

After the execution of the method Test.funk(bird, tiger), the value of bird is not changed - it remains with the value 10, even though in the funk()method we have changed the value with a = b[0] + 5;

执行该方法后Test.funk(bird, tiger),bird 的值不会改变——它仍然保留在该值中10,即使在该funk()方法中我们已将值更改为a = b[0] + 5;

On the other hand, the value of the element in the array changes, because we have the statement b[0] = b[0] * 2;

另一方面,数组中元素的值会发生变化,因为我们有语句 b[0] = b[0] * 2;

I don't understand why one thing changes and the other not? Could someone please explain this for me.

我不明白为什么一件事会改变而另一件事没有?有人可以为我解释一下。

回答by Johannes Schaub - litb

Look at Jon Skeet's article about Parameter-Passing in Java, which explains this.

看看 Jon Skeet 的关于Java 中参数传递的文章,它解释了这一点。

In short (look at his site for a more throughout explanation):

简而言之(查看他的网站以获得更全面的解释):

Arrays are reference types. If you pass a reference that points to an array, the value of the reference is copied and assigned to the parameter of the function. So the parameter will point to the same array as the argument that was passed. Thus changes you make to the array through the parameter of your function will be visible in the calling function. Changing the parameter itself (b), for example by setting it to null, however, will not be noticed by the calling function, since the parameter (b) is just a copy of the argument (tiger) passed.

数组是引用类型。如果传递指向数组的引用,则引用的值将被复制并分配给函数的参数。因此参数将指向与传递的参数相同的数组。因此,您通过函数参数对数组所做的更改将在调用函数中可见。更改参数本身 (b),例如将其设置为null,但是调用函数不会注意到,因为参数 (b) 只是传递的参数 (tiger) 的副本。

Integers are so-called primitive types. Passing the integer copies its value and assigns it to the parameter too. But that value is not a reference to the actual data, but is the data itself. So changes to the paramter in the function will affect the parameter (a), but not the argument passed in the calling function (bird).

整数是所谓的原始类型。传递整数会复制其值并将其分配给参数。但该值不是对实际数据的引用,而是数据本身。因此,函数中参数的更改会影响参数(a),但不会影响调用函数(bird)中传递的参数。

回答by OscarRyz

That's because when you declare

那是因为当你声明

 public static void funk(int a, int[] b) 

The scope of the variable ais only that method. Then when you change the value, you are changing only the value of thatvariable in thatmethod.

变量a的作用域只是那个方法。然后,当您更改该值时,您只会更改方法中变量的值。

About the b. Thats a new object reference to the same array created in main, that's why it seemsthe value does change ( what is changing is the array object underneath )

关于 b. 这是对在 main 中创建的同一个数组的新对象引用,这就是为什么值似乎确实发生了变化(变化的是下面的数组对象)

But try this:

但是试试这个:

public static void funk(int a, int[] b) { 
    // create a new reference for b
    int[] c = new int[b.length];
    c[0] = b[0];
    b = c;

    // The same.
    b[0] = b[0] * 2; 
    a = b[0] + 5;
} 

When you do that you'll the the value of tiger didn't change either ( only the content of the new array c created in funk )

当你这样做时,tiger 的值也不会改变(只有在 funk 中创建的新数组 c 的内容)

You can simulate the pass by ref using a wrapper. See this post.

您可以使用包装器模拟通过 ref 传递。看到这个帖子。

Although I haven't got any comments about that

虽然我对此没有任何评论

EDIT Just for fun:

编辑只是为了好玩:

I have modified your code to use the wrapper posted above.

我已经修改了您的代码以使用上面发布的包装器。

It looks pretty strange but looks like it works.

它看起来很奇怪,但看起来很有效。

// By ref simulated.
public class Test {

    public static void funk(_<Integer> a, int[] b) { 
        b[0] = b[0] * 2; 
        a.s(  b[0] + 5 ) ;
    } 

    public static void main(String[] args) {
        _<Integer> bird = new _<Integer>(10);
        int[] tiger = {7};

        Test.funk( bird , tiger );

        System.out.println("bird = " + bird );
        System.out.println("tiger = " + tiger[0] );

    }

}

Prints

印刷

bird = 19
tiger = 14

:-S

:-S

回答by stephendl

Basically, objects (like arrays) are passed into methods "by reference". So when you change the object, it changes the same object that was passed into the method.

基本上,对象(如数组)通过“引用”传递给方法。因此,当您更改对象时,它会更改传递给方法的同一对象。

Primitives (like int) are "passed by value", so the variable you are assigning a value to in a is not the same as the int variable that was passed in.

基元(如 int)是“按值传递的”,因此您在 a 中赋值的变量与传入的 int 变量不同。

I hope this helps...

我希望这有帮助...

回答by kjv.007

One variable is passing by reference and the other is by value :)

一个变量通过引用传递,另一个变量通过值:)

What's the difference between passing by reference vs. passing by value?

按引用传递与按值传递有什么区别?