Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is null typed in Java [duplicate]

Tags:

java

types

null

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.

like image 641
nikhil Avatar asked Mar 11 '15 18:03

nikhil


People also ask

Is null a double Java?

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.)

IS null same in Java?

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.

What is null type in Java?

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.

Is null and false same 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).

What is the default value of null in Java?

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?

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.

Can null be cast to a primitive type?

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.

Can We pass Null to object reference variable in Java?

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.


1 Answers

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 type Boolean, then unboxing conversion converts r into r.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 a true value if the string argument is not null and is equal, ignoring case, to the string "true".

like image 155
Sotirios Delimanolis Avatar answered Sep 18 '22 21:09

Sotirios Delimanolis