+ 1
Can I have a Double (object) with null value in Java?
When we use primitive values, they have a default value. With them wrappers can we have default values or them initialization have null values?
2 Respostas
+ 3
As with primitive variables, reference variables default to null.
Again, as with primitives, reference variables only default to null if they're global variables and not local. local primitives and reference variables do not initialise by default and will cause a compile time error if unassigned.
Check out the code below. You'll notice the only one to thrown an error is the uninitialised local variable.
https://code.sololearn.com/c73g3f1vV9Wg
+ 2
A quick test in Code Playground showed that a Double being uninitialized triggers warning, while it showed null (no warning) for the one that was initialized with null.