我正在研究我的Java以准备考试,我遇到了一些未初始化的int/Integer值的问题.
class A
{
int x;
Integer y;
static int z;
static Integer z2;
public A(){}
}
Run Code Online (Sandbox Code Playgroud)
假设我初始化了A类的对象.A a = new A();
我在编译器中尝试了这个并得到了结果
a.x == 0; true
a.x == null; Static Error: Bad type in comparison expression
a.y == 0; java.lang.NullPointerException
a.y == null; true
a.z == 0; true
a.z == null; Static Error: Bad type in comparison expression
a.z2 == 0; NullPointerException
a.z2 == null; true
Run Code Online (Sandbox Code Playgroud)
此外,我在交互窗格中尝试了一些更未初始化的int/Interger比较,看看如果我的x,y不是上面的类实例变量,我是否会得到不同的结果.
int x;
Integer y;
x == 0; true
x == null; Static …Run Code Online (Sandbox Code Playgroud)