Java ternary operator details explained

Java ternary operator details explained

@author:Jingdai
@date:2020.09.24

Seeing the title, you must think there is something to say about the ternary operator, isn't it very simple. I thought the same way before. Until today, I encountered a little problem with LeetCode, and found that there are still some details that I don't know, so I will summarize it.

Short circuit problem

First, we all know in Java &&and ||the short-circuit operation is the effect, if the first part of the logical operator can determine the result of the operation, we will not go to calculate the second part of the logical operator, such as the following code, the output istill It is 1, because the subsequent calculation is short-circuited, and there is no calculation.

int i = 1;
if (true || (i++) > 5) {
     
     
   System.out.println("test");
}
System.out.println(i);   // 1

Back to the ternary operator, does the ternary operator also have a short-circuit effect? Look at the code below.

int i = 5;
int j = true ? 1 : ++i;
System.out.println(i);  // 5

5 result output is described ternary operator has a short-circuit problem similar logical operator, directly to trueand falsecorresponding to the result take place without accounting for the other part, so that even if the code is written as a result, the output result is 5, not Change because of the order.

int i = 5;
int j = false ? ++i : 1;
System.out.println(i);  // 5

The order of operations

First look at the following code.

int[] array = new int[5];
int i = 4;
boolean flag = i++ > 4 ?  true : false;
System.out.println(flag);          // false
i = 4;
int b = i++ > 3 ?  array[i] : array[i];

We know that i++is the first increase since then the assignment, so the flagresult is falsewell understood, but the following bnumber of results is it? Look directly at the results of the operation.

false
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
        at A.main(A.java:16)

The result is an array bounds exception is thrown, results show that when comparing the end, ithe value has been added, that at this time i = 5, time to go get the value of the array will throw an array bounds exception, so do not think that ivalue throughout It will increase after the expression ends.

Packaging issues

When using the ternary operator to deal with packaging classes, pay attention to the NPE problem, see the code below.

int a = 5;
Integer b = null;

int c = true ? a : b;
System.out.println(c);    // 5

In this example, it will output 5, there is no problem, but the code is slightly changed, see the code below.

int a = 5;
Integer b = null;

int c = true ? b : a;
System.out.println(c);

Is the only aand bexchanged positions will report to run NullPointerException, this is why. It is also well understood, when is truethe time, will be selected b, because the front cis the basic data type, so when the packaging is automatically unpacking, into code like, and bis null, it will throw an NullPointerExceptionexception.

int a = 5;
Integer b = null;

int c = true ? b.intValue() : a;
System.out.println(c);

However, the code will become like the following, and normally should not be a problem, because cthat type of packaging, can receive null, but will still happen runtime NullPointerExceptionexception. In fact, when the second and third operands of the ternary operator are the basic type of packaging and basic data types, and the packaging type is selected, the packaging type will automatically perform unboxing operations. That is, the code will still be unpacking operation, it will happen NullPointerExceptionabnormal, it is very easy to make a mistake.

int a = 5;
Integer b = null;

Integer c = true ? b : a;
// 还是会变成Integer c = true ? b.intValue() : a;
System.out.println(c);

Guess you like

Origin blog.csdn.net/qq_41512783/article/details/108781823