25

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.

José Luis
  • 1,816
  • 8
  • 24
  • 26
Kevin Zhou
  • 1,273
  • 4
  • 17
  • 25
  • 18
    If you're wondering who's more credible in knowing what the compiler will do: the compiler or your professor...the answer is the compiler. – Mark Peters Oct 08 '10 at 19:25
  • In the case of "not class instance variables", the line x == 0 would result in a compile-time error, I believe. You'd get: "Error: The local variable myValue may not have been initialized" – CSP.AT.MASH Feb 02 '23 at 18:41

9 Answers9

21
  • 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
  • 278,309
  • 50
  • 514
  • 539
19

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

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Stan Kurilin
  • 15,614
  • 21
  • 81
  • 132
11

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.

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
Nabb
  • 3,434
  • 3
  • 22
  • 32
2
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
  • 11,542
  • 1
  • 25
  • 31
2
class A
{
   int x;
   Integer y;
   static int z;
   static Integer z2;
   public A(){}   
}

your compiler says

x == 0; true;
x == null; Static Error: Bad type in comparison expression
y == 0; java.lang.NullPointerException
y == null; true

your teacher says

x == 0; Uninitialized
x == null; Undefined
y == 0; java.lang.NullPointerException
y == null; Uninitialized

Both are correct, except your teacher uses different terms. The reason is that by default JAVA initialize uninitialized values to 0 or null for objects. Your teacher refers to them as uninitialized. He is right because these values have not been initialized yet (but they still have default values). Your teacher wants to teach you to always initialize your variables because it's good practice.

vincent456
  • 102
  • 5
1

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
  • 2,343
  • 1
  • 27
  • 38
  • "Unitialized objects equals null" -I don't think so. – Stan Kurilin Oct 08 '10 at 19:20
  • 1
    "Unitialized int equals 0. Unitialized Integer equals null." Not true, only when they are instance variables not local variables. – Johnbabu Koppolu Oct 08 '10 at 19:23
  • @Stas. Give me one example of a unitialized object that's not null. – pablosaraiva Oct 08 '10 at 19:28
  • @pablosaraiva: "A equals B" means a.equal(b) == true, imho. – Stan Kurilin Oct 08 '10 at 19:32
  • @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 Oct 08 '10 at 19:36
  • Yes @Matthew, I've agreed already with @johnbk. Concerning Instance variables, class variables and array componets what I said is valid. – pablosaraiva Oct 08 '10 at 19:39
  • @pablosaraiva: you can use "the value is null" instead "equals null". – Stan Kurilin Oct 08 '10 at 19:45
  • @Stas The java equality operator is read "equals to" also. So, if unitialized objects (besides locals) == null, I can say they equals null. – pablosaraiva Oct 08 '10 at 19:49
  • @pablosaraiva : Ok. Probably, it can be my mistake. – Stan Kurilin Oct 08 '10 at 19:51
1

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.]

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
JeffW
  • 311
  • 1
  • 2
  • 6
0

This question was asked a while back, and there are correct answers provided, however, I feel that they can be expanded somewhat.

I would like to quote a couple a lines from the official tutorials page. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Fields that are declared but not initialized will be set to a reasonable default by the compiler

Local variables are slightly different; the compiler never assigns a default value to an uninitialized local variable. Accessing an uninitialized local variable will result in a compile-time error.

In Java primitives have a value which is a number. (its not quite as simple and I do urge you to read more about it.) A value for an object is a reference from where it is possible to find the contents of the object.

Default value for a primitive is essentially a 0 where as default value for an object is null. (when uninitialized and when a field)

In your example, you try to compare "0 to 0, null to null and null to 0".

Fact is: null != 0.

  • 0 = a numeric value representing nothing.
  • null = a literal for representing non existing reference. (you may see What is null in Java? for more details on null)

FYI: I believe this question was answered splendidly by Matthew Flaschen already, I merely wanted to add extra info for those who are interested.

Community
  • 1
  • 1
Atspulgs
  • 1,359
  • 11
  • 9
0

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!!!