为什么使用默认值的 Java Integer 会导致 NullPointerException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2437603/
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
Why does using a default-valued Java Integer result in a NullPointerException?
提问by user292844
I am new to Java. I just read that class variables in Java have default value.
我是 Java 新手。我刚刚读到 Java 中的类变量具有默认值。
I tried the following program and was expecting to get the output as 0
, which is the default value on an integer, but I get the NullPointerException
.
我尝试了以下程序,并希望得到输出为0
,这是整数的默认值,但我得到了NullPointerException
.
What am I missing?
我错过了什么?
class Test{
static Integer iVar;
public static void main(String...args) {
System.out.println(iVar.intValue());
}
}
采纳答案by codaddict
You are right, uninitialized class variables in Java have default value assigned to them. Integer
type in Java are not same as int
. Integer
is the wrapper class which wraps the value of primitive type int
in an object.
您是对的,Java 中未初始化的类变量具有分配给它们的默认值。Integer
Java 中的类型与int
. Integer
是包装类,它将原始类型的值包装int
在一个对象中。
In your case iVar
is a reference to an Integer
object which has not been initiliazed. Uninitialized references get the default value of null
and when you try to apply the intValue
() method on a null reference you get the NullPointerException
.
在您的情况下iVar
是对Integer
尚未初始化的对象的引用。未初始化的引用获得默认值,null
当您尝试对intValue
空引用应用() 方法时,您将获得NullPointerException
.
To avoid this problem altogether you need to make your reference variable refer to an Integer
object as:
为了完全避免这个问题,你需要让你的引用变量引用一个Integer
对象:
class Test {
// now iVar1 refers to an integer object which wraps int 0.
static Integer iVar1 = new Integer(0);
// uninitialized int variable iVar2 gets the default value of 0.
static int iVar2;
public static void main(String...args) {
System.out.println(iVar1.intValue()); // prints 0.
System.out.println(iVar2); // prints 0.
}
}
回答by Jason Coco
It means that iVar
is null. In java, you can't invoke methods on a null reference, it generates the NullPointerException that you are seeing.
这意味着它iVar
是空的。在 java 中,您不能在空引用上调用方法,它会生成您看到的 NullPointerException。
回答by SunilChandra M N
private Integer amount=Integer.valueOf(0);
private Integer points=Integer.valueOf(0);
In particular, why you should use Integer.valueOf(int)
instead of new Integer(int): CACHING.
特别是,为什么你应该使用Integer.valueOf(int)
而不是 new Integer(int): CACHING。
This variant of valueOf
was added in JDK 5 to Byte, Short, Integer, and Long (it already existed in the trivial case in Boolean since JDK 1.4). All of these are, of course, immutable objects in Java. Used to be that if you needed an Integer object from an int, you'd construct a new Integer. But in JDK 5+, you should really use valueOf because Integer now caches Integer objects between -128 and 127 and can hand you back the same exact Integer(0) object every time instead of wasting an object construction on a brand new identical Integer object
这个变体valueOf
在 JDK 5中被添加到 Byte、Short、Integer 和 Long(自 JDK 1.4 以来,它已经存在于 Boolean 的平凡案例中)。当然,所有这些都是 Java 中的不可变对象。过去,如果你需要一个 int 中的 Integer 对象,你会构造一个新的 Integer。但是在 JDK 5+ 中,您应该真正使用 valueOf,因为 Integer 现在缓存 -128 到 127 之间的 Integer 对象,并且每次都可以将完全相同的 Integer(0) 对象交还给您,而不是在全新的相同 Integer 对象上浪费对象构造