如何增加类Integer从另一个方法引用java中的值 [英] How to Increment a class Integer references value in java from another method

查看:215
本文介绍了如何增加类Integer从另一个方法引用java中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package myintergertest;

/**
 *
 * @author Engineering
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        //this one does not increment 
        Integer n = new Integer(0);
        System.out.println("n=" + n);
        Increment(n);
        System.out.println("n=" + n);
        Increment(n);
        System.out.println("n=" + n);
        Increment(n);
        System.out.println("n=" + n);
        Increment(n);

        //this one will increment
        MyIntegerObj myInt = new MyIntegerObj(1);
        Increment(myInt);
        System.out.println("myint = " + myInt.get());
        Increment(myInt);
        System.out.println("myint = " + myInt.get());
        Increment(myInt);
        System.out.println("myint = " + myInt.get());

    }

    public static void Increment(Integer n) {
        //NO.  this doesn't work because a new reference is being assigned
        //and references are passed by value in java
        n++;
    }

    public static void Increment(MyIntegerObj n) {
        //this works because we're still operating on the same object
        //no new reference was assigned to n here.
        n.plusplus();   //I didn't know how to implement a ++ operator...
    }
}

所有这些的结果是n = 0。整数n是一个对象,因此通过引用传递,那么为什么增量不会反映在调用方法(main)中?我期望输出为n = 0 n = 1 n = 2等...

The result for all of those is n=0. Integer n is an object and therefore passed by reference, so why isn't the increment reflected back in the caller method (main)? I expected output to be n=0 n=1 n=2 etc...

更新:
注意我更新了上面的代码示例。如果我理解正确,Jon Skeet回答了为什么myInt会增加以及为什么n不会增加的问题。这是因为n正在获取在Increment方法中指定的新引用。但myInt没有被分配一个新的引用,因为它正在调用一个成员函数。

UPDATE: Notice I updated the code example above. If I'm understanding correctly, Jon Skeet answered the question of why myInt would increment and why n does not. It is because n is getting a new reference assigned in the Increment method. But myInt does NOT get assigned a new reference since it's calling a member function.

这听起来像我理解正确吗?

Does that sound like I understand correctly lol ?

推荐答案

不,对象不通过引用传递。参考文献通过价值传递 - 这是一个很大的不同。 Integer 是一个不可变类型,因此您无法更改方法中的值。

No, objects aren't passed by reference. References are passed by value - there's a big difference. Integer is an immutable type, therefore you can't change the value within the method.

您的 n ++; 语句实际上是

n = Integer.valueOf(n.intValue() + 1);

因此,它为变量 n in Increment - 但由于Java 具有按值传递,因此不会影响 n 在调用方法中。

So, that assigns a different value to the variable n in Increment - but as Java only has pass-by-value, that doesn't affect the value of n in the calling method.

编辑:回答你的更新:那是对的。据推测,您的MyIntegerObj类型是可变的,并在您调用 plusplus()时更改其内部状态。哦,不要四处寻找如何实现运算符--Java不支持用户定义的运算符。

To answer your update: that's right. Presumably your "MyIntegerObj" type is mutable, and changes its internal state when you call plusplus(). Oh, and don't bother looking around for how to implement an operator - Java doesn't support user-defined operators.

这篇关于如何增加类Integer从另一个方法引用java中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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