I've encountered a bug in my codebase, I've narrowed down to what caused this behavior. The first test case fails, whereas the last two succeed.
@Test public void testBooleanNull1() { Boolean nullB = null; assertFalse(Boolean.valueOf(nullB)); } @Test public void testBooleanNull2() { String nullS = null; assertFalse(Boolean.valueOf(nullS)); } @Test public void testBooleanNull3() { assertFalse(Boolean.valueOf(null)); }
I know that Boolean.valueOf
is an overloaded method with two variants one takes a String
and the other takes a primitive of type boolean
.
I suspect that this is happening because of auto-boxing but I'm not sure if that is the case, furthermore I don't know why null
is being converted to a Boolean
as far as I know null
is not a valid primitive
type.
I've moved on to using BooleanUtils
from Apache Commons
, I asked this here to better understand why the behavior is this way.
Firstly, a Java double cannot be null, and cannot be compared with a Java null . (The double type is a primitive (non-reference) type and primitive types cannot be null.)
The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.
Null keyword Null is a reserved keyword in the Java programming language. It's technically an object literal similar to True or False. Null is case sensitive, like any other keyword in Java.
You can't compare null to a boolean . They are different types one indicating a null reference pointer and the other a false/not true value. Thus the answer is: No this expression is not even valid (and if it was, it would be false).
2. Reference Variable value: Any reference variable in Java has a default value null . 3. Type of null: Unlike the common misconception, null is not Object or neither a type. It’s just a special value, which can be assigned to any reference type and you can type cast null to any type Examples:
Is null a literal in Java? The null in Java is a literal of the null type. It cannot be cast to a primitive type such as int. float etc. but can be cast to a reference type. Also, null does not have the value 0 necessarily.
The null in Java is a literal of the null type. It cannot be cast to a primitive type such as int. float etc. but can be cast to a reference type. Also, null does not have the value 0 necessarily.
The access to a null reference generates a NullPointerException. It is not allowed to pass null as a value to call the methods that contain any primitive data type. Let's see a simple example to display the default value of the reference variable. Let's see an example to determine whether we can pass null to object reference variable.
This
Boolean.valueOf(nullB)
is an invocation of Boolean#valueOf(boolean)
.
It fails because the unboxing of the Boolean
value nullB
fails with a NullPointerException
. In other words, it becomes
boolean val = nullB.booleanValue(); // at runtime nullB stores the value null Boolean.valueOf(val)
This process is described in the JLS here
If
r
is a reference of typeBoolean
, then unboxing conversion convertsr
intor.booleanValue()
This
Boolean.valueOf(null)
is invoking the overloaded version that accepts a String
(since null
is not a valid expression of type boolean
).
Returns a
Boolean
with a value represented by the specified string. The Boolean returned represents atrue
value if the string argument is notnull
and is equal, ignoring case, to the string"true"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With