Alex Rivera | Logout

Uninitialized int vs Integer

Asked 2010-10-08T19:08:25.350
26

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.

Edit
Report

1 Answer

0

EDIT: Uninitialized local variables can't be used.

Besides locals:

Unitialized int equals 0.

Unitialized Integer equals null.

Integer is an Object. Unitialized objects equals null.

int is a primitive type. The language specs define it's uninitialized value is 0.

answered 2010-10-08T19:17:23.857

Your Answer