If the following code is possible:
Integer a = null;
int b = a;
Does it mean that a function returning a possible null value for an integer is a bad practice?
Edit 1:
There are several different opinions in these answers. I am not enough confident to choose one or another.
That code will give a
NullPointerExceptionwhen you run it. It’s basically equivalent to:… which makes it clearer that it will indeed fail.
You’re not really assigning a null value to an
int– you’re trying and failing.It’s fine to use
nullas a value for anInteger; indeed oftenIntegeris used instead ofintprecisely as a “nullable equivalent`.