Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Integer对象在方法中正确实例化,但返回null_Java - Fatal编程技术网

Java Integer对象在方法中正确实例化,但返回null

Java Integer对象在方法中正确实例化,但返回null,java,Java,我在EJB类中使用java方法时遇到了一种奇怪的行为 我有几个整数,声明如下: Integer decimalDigit = null; Integer decimalExponent = null; 我将它们与其他参数一起传递给以下方法 public void GetPrecision(Currency cur, String nodeCode, Integer decimalDigit, Integer decimalExponent) { decimalDigit = new

我在EJB类中使用java方法时遇到了一种奇怪的行为

我有几个整数,声明如下:

Integer decimalDigit = null;
Integer decimalExponent = null;
我将它们与其他参数一起传递给以下方法

public void GetPrecision(Currency cur, String nodeCode, Integer decimalDigit, Integer decimalExponent) {

    decimalDigit = new Integer(cur.getDecimalDigit());
    decimalExponent = new Integer(cur.getDecimalExponent());

    if (!CommonHelper.isNullOrEmptyOrBlank(nodeCode)) {

        Node tempNode = nodeListProvider.getNodeList().get(nodeCode);

        if (tempNode != null && tempNode.getDecimalDigit() != null) {
            decimalDigit = (int) tempNode.getDecimalDigit();
            decimalExponent = 0;
        }
    }
}
这两个对象在使用新操作符的方法中被正确地表示出来,它们一直保持这样直到调用结束,但是,一旦我退出,这两个变量再次为null

我无法解释这种行为,有什么提示吗


提前感谢

参数按值传递,但方法接收引用的副本,而不是直接接收整数的引用
因此,在方法中对参数的任何赋值都不会更改您传递的引用引用的值。

您的方法应该返回一个结构实例(数组或自定义类),其中包含两个
整数


除此之外,名为
GetPrecision()
的方法应该会返回一些内容。

如果cur.getDecimalDigit()和cur.getDecimalExponent()都生成null,那么decimalDigit和decimalExponent在任何后续的值集上仍将为null。因此,请检查cur.getDecimalDigital()和cur.getDecimalExponent()值。

您指的是局部变量,其范围以以下行的方法结束:

decimalDigit = new Integer(cur.getDecimalDigit());
decimalExponent = new Integer(cur.getDecimalExponent());
因为您已经将它们声明为形式参数

public void GetPrecision(Currency cur, String nodeCode, Integer decimalDigit, Integer decimalExponent) {

    decimalDigit = new Integer(cur.getDecimalDigit());
    decimalExponent = new Integer(cur.getDecimalExponent());

    if (!CommonHelper.isNullOrEmptyOrBlank(nodeCode)) {

        Node tempNode = nodeListProvider.getNodeList().get(nodeCode);

        if (tempNode != null && tempNode.getDecimalDigit() != null) {
            decimalDigit = (int) tempNode.getDecimalDigit();
            decimalExponent = 0;
        }
    }
}
重新编写解决问题的方法:

public void GetPrecision(Currency cur, String nodeCode, Integer a, Integer b) {

//method body   

}

使用像
a
b
这样的标识符是一种不好的做法,但是您会遇到问题。

Java是按值传递的。当方法退出时,对传入的实际参数所做的更改不会保存。mmm可能重复,我不完全理解传递2整数和传递包含2整数的自定义类之间的区别。不是这两个都是对象吗?不是。我说你的方法应该返回一些东西:要么是一个数组
Integer[]
,它包含新的小数位数和小数指针值,要么是一个自定义类,它包含两个整数字段。不同的是,你传入的自定义对象的引用也没有更改。更改的是引用引用的对象内的整数。因此,您不能更改引用,但可以更改引用引用的对象。David Choweller是对的。但我提出的想法只是在方法中返回自定义对象,以便在方法中调用后可以检索它<代码>精度精度=getPrecision(cur、nodeCode、小数位数、小数指针)。从
void
更改为
Precision
,并在方法中返回精度。