user1839489
user1839489

Reputation:

Is no initialization the same as initialization to null?

If you declare an object like so:

Object x;

does it initialize as null?

For example, will (x == null) be true?

Upvotes: 3

Views: 141

Answers (2)

arshajii
arshajii

Reputation: 129507

If x is a field, it will be null by default. If it is a local variable, you have to explicitly initialize it before you use it. This is outlined in JLS §4.12.5: Initial Values of Variables.

Upvotes: 11

Rahul Tripathi
Rahul Tripathi

Reputation: 172438

member variables are initialized to null and local variables are not initialized and the initial value is set by you.

Note:- null is definitely a value. Its the value which doesnt refer to any object.

Upvotes: 1

Related Questions