Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does returning null for a primitive work in this case?

Tags:

java

jls

This ugly piece of code does compile but throws NPE if s == null

public static boolean isNullOrEmpty(String s)
{
    return s != null ? s.isEmpty() : null;
}

while this does not (as expected):

public static boolean isNullOrEmpty(String s)
{
    if(s != null)
        return s.isEmpty();
    else
        return null;
}

I know both of them are plainly wrong, but as I found the first piece of code in our sources, I was quite surprised it did compile.

Edit: Here's the relevant part of the JLS from Java 7. I guessed the first statement would apply but the bold one does.

15.25 Conditional Operator ? :

[...]

The type of a conditional expression is determined as follows:

[...]

  • If one of the second and third operands is of primitive type T, and the type of the other is the result of applying boxing conversion (§5.1.7) to T, then the type of the conditional expression is T.

[...]

  • Otherwise, the second and third operands are of types S1 and S2 respectively. Let T1 be the type that results from applying boxing conversion to S1, and let T2 be the type that results from applying boxing conversion to S2. The type of the conditional expression is the result of applying capture conversion (§5.1.10) to lub(T1, T2) (§15.12.2.7).
like image 417
atamanroman Avatar asked Nov 29 '12 12:11

atamanroman


People also ask

Can a primitive type be null?

Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.

What happens if you return null?

Returning null is often a violation of the fail fast programming principle. The null can appear due to some issue in the application. The issue can even go to production if the developer has not implemented proper exception handling, which can help quickly detect the issue.

What does returning null do in Java?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type. In the absence of a constructor, the getArticles() and getName() methods will return a null reference.

What does it mean to return null?

Returning null is usually the best idea if you intend to indicate that no data is available. An empty object implies data has been returned, whereas returning null clearly indicates that nothing has been returned.


1 Answers

The first has a ternary operator which has a result type of Boolean. The NPE is converting a null to a boolean.

It is actually something like:

Boolean temp = s != null ? s.isEmpty() : null; //no problems here
return temp; //crash when temp==null

The second is trying to return a wrong type (Object instead of primitive) - and thus does not compile.

like image 118
amit Avatar answered Sep 30 '22 00:09

amit