views:

158

answers:

7

Hi all, 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.

+2  A: 

Java values of simple types such as int/long can't be null. So they initialized by 0.

Stas
Why was this downvoted? It's right!
Mark Peters
@Mark Peters: Thanks.
Stas
+1  A: 

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.

pablosaraiva
"Unitialized objects equals null" -I don't think so.
Stas
"Unitialized int equals 0.Unitialized Integer equals null." Not true, only when they are instance variables not local variables.
johnbk
@Stas. Give me one example of a unitialized object that's not null.
pablosaraiva
@pablosaraiva: "A equals B" means a.equal(b) == true, imho.
Stas
@johnbk is right. Unitialized local variables can't be used.
pablosaraiva
@pablo, the default values - [`§4.12.5`](http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#96595) in the spec - apply only to "class variable, instance variable, and array component[s]". The standard doesn't provide for initializing local variables to null automatically. It would be unnecessary and inefficient, since there has to be an explicit initialization.
Matthew Flaschen
Yes @Matthew, I've agreed already with @johnbk. Concerning Instance variables, class variables and array componets what I said is valid.
pablosaraiva
@pablo, then you should edit your answer.
Matthew Flaschen
@Matthew Done...
pablosaraiva
@pablosaraiva: you can use "the value is null" instead "equals null".
Stas
@Stas The java equality operator is read "equals to" also. So, if unitialized objects (besides locals) == null, I can say they equals null.
pablosaraiva
@pablosaraiva : Ok. Probably, it can be my mistake.
Stas
+1  A: 
int x;
Integer y;
x == 0; true. because x is initialized to 0 by JVM
x == null; Static Error: Bad type in comparison expression
y == 0; java.lang.NullPointerException
y == null; true, because y is uninitialized
Ishtar
A: 

All the objects/variables in a class are initialized to default values when an object is instantiated.

Thats the reason, the variables inside the class have the following values:

int x = 0 //default value (value type)
Integer y = null //default values of any object; here Integer is reference type

... and the rest goes on similarly. Hope my answer is useful!!!

Guru Charan
+5  A: 

In Java, class (static) variables, instance variables (those in your example), and array components are given default values. Local variables on the other hand must be given values explicitly and do not get default values.

For more detail, see §4.12.5.

Nabb
This is from an outdated JLS, might want to cite version 3 (see JeffW's answer)
Mark Peters
@Mark: Thanks. Updated it.
Nabb
+1  A: 

This one has bugged me before since descriptions and behavior seems a little inconsistent. If you look at the language specification in section 4.12.5 there is a section that describes this and does jive with what you observed the compiler doing.

The reason I think this is confusing at times is that other publications I have read from Sun ("Core Java 2" for example) describe the behavior that your prof indicated. And in another variation, I use NetBeans which allows the use of uninitialized primitives but flags the use of uninitialized objects; I am not sure if that is a compiler or an IDE choice though.

[EDIT : after reviewing one of the posts, I believe that this confusion does stem from the different behavior for local variables vs fields.]

JeffW
A tip: cite the HTML version of the JLS, not the pdf version.
Mark Peters
Good point. Fixed.
JeffW
+2  A: 
  • a.x == 0 - True because a.x has a default value of 0.
  • a.x == null - As noted, this is a compile-time error. This follows from §15.21.3: "A compile-time error occurs if it is impossible to convert the type of either operand to the type of the other by a casting conversion (§5.5)." The null type isn't convertible to a number.
  • a.y == 0 - This tries to unbox a.y, which is null, so it throws a NullPointerException. Unlike the above (which has a literal null), the compiler doesn't try to figure out at compile-time that a.y will be null.
  • a.y == null - Again, true because a.y is initialized to null
  • a.z == 0 - Same as a.x (except static)
  • a.z == null - Same as a.x (except static)
  • a.z2 == 0 - Same as a.y (except static)
  • a.z2 == null - Same as a.y (except static)

The problem with the interactions pane is that it's up to the IDE how to implement it. If x and y are local (uninitialized) variables, all four of your last comparisons will fail to compile.

Matthew Flaschen