Can I have a Double (object) with null value in Java? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

20th Aug 2018, 1:27 PM
Andrés Felipe Moreno Torres
Andrés Felipe Moreno Torres - avatar
2 Answers
+ 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
20th Aug 2018, 9:07 PM
Steven Trippier
Steven Trippier - avatar
+ 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.
20th Aug 2018, 1:42 PM
Ipang