Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-unboxing need of ternary if-else

Tags:

This piece of code works fine :-

    Integer nullInt = null;     if (1 <= 3) {         Integer secondNull = nullInt;     } else {         Integer secondNull = -1;     }     System.out.println("done"); 

But this throws null-pointer exception, while Eclipse warning that there is need for auto-unboxing :-

    Integer nullInt = null;     Integer secondNull = 1 <= 3 ? nullInt : -1;     System.out.println("done"); 

Why is that so, can somebody guide please?

like image 375
91StarSky Avatar asked Mar 04 '20 11:03

91StarSky


People also ask

Can ternary operator be used in if-else?

The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way. First, you need to write a conditional expression that evaluates into either true or false .

Why ternary operator is better than if-else?

Emphasis: value-selection before/after action needing values. There's a different emphasis: An if / else statement emphasises the branching first and what's to be done is secondary, while a ternary operator emphasises what's to be done over the selection of the values to do it with.

Why ternary operator should not be used?

Except in very simple cases, you should discourage the use of nested ternary operators. It makes the code harder to read because, indirectly, your eyes scan the code vertically.

Is ternary faster than if-else Python?

The fastest is the basic ternary operator, followed by the if-else statement and then by the ternary expression with a tuple.

What is autoboxing and unboxing in Python?

Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The technique let us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.

What is unboxing in C++?

Assigned to a variable of the corresponding wrapper class. Unboxing: Converting an object of a wrapper type to its corresponding primitive value is called unboxing. For example conversion of Integer to int.

What are the advantages of autoboxing and unboxing?

Advantages of Autoboxing / Unboxing: Autoboxing and unboxing lets developers write cleaner code, making it easier to read. The technique let us use primitive types and Wrapper class objects interchangeably and we do not need to perform any typecasting explicitly.

What are some examples of auto and unboxing in C++?

Another example of auto and unboxing is to find sum of odd numbers in a list. Important point in the program is that the operators remainder (%) and unary plus (+=) operators do not apply to Integer objects.


1 Answers

The type of the ternary conditional expression

1 <= 3 ? nullInt : -1 

is int (the JLS contains several tables that describe the type of the ternary conditional operator depending on the types of the 2nd and 3rd operands).

Therefore, when it tries to unbox nullInt to an int, a NullPointerException is thrown.

In order to get the behavior of your if-else snippet, you need to write:

1 <= 3 ? nullInt : Integer.valueOf(-1) 

Now the type of the expression will be Integer, so no unboxing will take place.

like image 83
Eran Avatar answered Sep 28 '22 06:09

Eran