简体   繁体   English

将Integer的引用传递给Java中的方法

[英]Passing reference of an Integer to method in Java

I've been reading this topic: Is Java "pass-by-reference" or "pass-by-value"? 我一直在阅读这个主题: Java是“按引用传递”还是“按值传递”? , and I've tried to run example from this answer: https://stackoverflow.com/a/73021/2838739 . ,并且我尝试从以下答案中运行示例: https : //stackoverflow.com/a/73021/2838739 In fact value of myDog has been changed outside this method. 实际上,myDog的值已在此方法之外更改。 Then I have tried to do the same with an Integer. 然后,我尝试对Integer执行相同操作。 I was surprised because its value has not been changed. 我很惊讶,因为它的价值没有改变。 Could someone explain me why? 有人可以解释一下为什么吗?

My test code is: 我的测试代码是:

   package testtest;

class Dog {

    String name;

    public Dog(String name) {
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

public class Main {

    public static void main(String[] args) {

        Dog myDog = new Dog("Rover");
        System.out.println(myDog.getName());
        foo(myDog);
        System.out.println(myDog.getName());

        Integer i = 5;
        changeValue(i);
        System.out.println(i);
    }

    public static void changeValue(Integer i) {
        i = 50;
    }
    public static void foo(Dog someDog) {
        someDog.setName("Max");     // AAA
        someDog = new Dog("Fifi");  // BBB
        someDog.setName("Rowlf");   // CCC
    }

}

And output was: 输出为:

Rover 流浪者

Max 马克斯

5

Thank you in advance. 先感谢您。

Integer is not a mutable class. 整数不是可变的类。 (There is no set method on it.) You can point an old variable to a different Integer, but that doesn't change the Integer that you passed to the method. (没有set方法。)您可以将旧变量指向另一个Integer,但这不会更改传递给该方法的Integer。

Integer is a primitive data type. 整数是原始数据类型。 You cannot change them inside methods. 您不能在方法内部更改它们。

If you pass an object to a method you can change its accessible member variables. 如果将对象传递给方法,则可以更改其可访问成员变量。

In Java there are special wrapper classes for primitive types like Integer for int, Character for char etc, and even though they are like objects you cannot change their values inside methods either. 在Java中,有一些原始类型的特殊包装器类,例如Integer用于int,Character用于char等,即使它们像对象一样,您也不能在方法内部更改其值。 That's the only exception that I know. 我知道这是唯一的例外。

As pointed out already primitive types are passed by value. 如前所述,原始类型已经通过值传递。 But if you really need to pass primitives by reference you can use an array. 但是,如果您确实需要通过引用传递基元,则可以使用数组。

public class PassByRefMain {

    public static void main(String[] args) {
        PassByRefMain m = new PassByRefMain();
        int[] number = new int[]{5};
        System.out.println(number[0]);
        m.changevalue(number);
        System.out.println(number[0]);  
    }

    public void changevalue(int [] number){
        number[0] = 9;
    }
}

The output will be 输出将是

5
9

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

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