I think you can do it by using the wrapper and boxing concepts
For example in your case you can create another method to receive two parameters and then do something like this:
add(int a, int b){
Boolean c = null;
add(a, b, c);
}
Or with an int value:
add(int a, boolean c){
Integer b = null;
add(a, b, c);
}
In that way you can change the primitive to its wrapper class... however, in the call it will throw and exception...
The reason is the auto-unboxing of the Integer b = null
into a int
. To unbox, the JVM will call int Integer.intValue()
on a null
value. To correct this exception, the solution is to prevent any unboxing, so the method needs to work with the wrappers classes. Here is the method :
add(Integer a, Integer b, Boolean c){
//...
}
Since the parameter are not primitive, there is no need to try unboxing the null
value. But this is needed inside of the method like any Object parameter. You still need to do some null
check before using any nullable
value. So you can't expect a + b
to return something if b
is null.