I was just studying up on my Java in preparation for an exam and I ran into a sort of problem with uninitialized int/Integer values.
class A
{
int x;
Integer y;
static int z;
static Integer z2;
public A(){}
}
Lets say I initialize an object of Class A. A a = new A();
I've tried this in a compiler and got the results
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
Furthermore , I tried some more uninitialized int/Interger comparisons in an interactions pane to see if I would get different results if my x, y were not class instance variables as they are above.
int x;
Integer y;
x == 0; true
x == null; Static Error: Bad type in comparison expression
y == 0; java.lang.NullPointerException
y == null; true
However, my professor claims in a lecture that the values should be as follows:
x == 0; Uninitialized
x == null; Undefined
y == 0; java.lang.NullPointerException
y == null; Uninitialized
Now I don't want to doubt the one who writes the exam, but which x == 0 and y == null truth value is correct? An explanation on why would be very much appreciated, thank you.
Java values of simple types such as int/long can't be null so they are initialized by 0.