Java中对原始类型的引用(如何强制原始数据保留在盒中)

Uri*_*Uri 5 java

我想将对原始类型的引用传递给方法,这可能会更改它。

考虑以下示例:

public class Main {

    Integer x = new Integer(42);
    Integer y = new Integer(42);

    public static void main(String[] args) {
        Main main = new Main();
        System.out.println("x Before increment: " + main.x);

        // based on some logic, call increment either on x or y
        increment(main.x);

        System.out.println("x after increment: " + main.x);
    }

    private static void increment(Integer int_ref) {
        ++int_ref;
    }
}
Run Code Online (Sandbox Code Playgroud)

运行示例的输出为:

x Before increment: 42
x after increment: 42
Run Code Online (Sandbox Code Playgroud)

这意味着int_ref是通过值而不是通过引用传递给函数的,尽管我的名字很乐观。

显然,有一些方法可以解决此特定示例,但是我的实际应用程序要复杂得多,并且通常可以想到,“指针”或对整数的引用在许多情况下都是有用的。

我试图将Object传递给函数(然后将其转换为int),以及其他各种方法,但是没有运气。一种似乎可行的解决方法是定义我自己的Integer类版本:

private static class IntegerWrapper {
    private int value;

    IntegerWrapper(int value) { this.value = value; }
    void plusplus() { ++value; }
    int getValue() { return value; }
}
Run Code Online (Sandbox Code Playgroud)

这样做并传递对IntegerWrapper的引用确实可以按预期工作,但是就我的口味而言,这似乎很la脚。来自C#,其中boxed变量只保留boxed,我希望我只是想念一些东西。

编辑:

我会说我的问题不是Java的“通过引用传递”还是“通过值传递”的重复,因为我的问题不是理论性的,因此我只是寻求解决方案。从哲学上讲,所有语言中的所有方法调用都是按值传递的:它们要么传递实际值,要么传递值的引用-按值。

因此,我将重述我的问题:解决Java中我无法传递对Integer的引用的问题的常见范例是什么?在已知的范例之上是否建议了IntegerWrapper?库中是否已经存在类似的类(也许是MutableInt)?也许长度为1的数组是一种常见的做法,并且具有一些性能优势?我是唯一一个对可以存储对任何对象(但基本类型)的引用感到不高兴的人吗?

ole*_*nik 6

Integer 您可能会注意到,它是一成不变的。

您采用的方法private static class IntegerWrapper是正确的。使用大小为1的数组也是正确的,但是在实践中,我从未见过在这种情况下使用数组。所以用IntegerWrapper.

您可以在Apache中找到完全相同的实现org.apache.commons.lang3.mutable.MutableInt

在您的示例中,您还可以为Main静态方法提供实例:

public class Main {

    private int x = 42;

    public static void main(String[] args) {
        Main main = new Main();
        incrementX(main);
    }

    private static void incrementX(Main main) {
        main.x++;
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,从Java8您可以定义一个inc函数并使用它来增加值:

public class Main {

    private static final IntFunction<Integer> INC = val -> val + 1;

    private int x = 42;

    public static void main(String[] args) {
        Main main = new Main();
        main.x = INC.apply(main.x);
    }

}
Run Code Online (Sandbox Code Playgroud)