Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is no initialization the same as initialization to null?

Tags:

java

If you declare an object like so:

Object x;

does it initialize as null?

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


2 Answers

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.

like image 116
arshajii Avatar answered Aug 17 '26 20:08

arshajii


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.

like image 22
Rahul Tripathi Avatar answered Aug 17 '26 21:08

Rahul Tripathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!