Ternary expression execution order

@Test

public void fun2(){
Studuent s=null;
String age=s==null?"0":s.getAge();
System.out.println(age);
}

The order of execution of the ternary expression: first compare the left side to obtain the Boolean value, and select the execution statement according to the Boolean value. As above, s.getAge() will wait for the Boolean value to be false to execute, and there will be no null pointer error;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325256510&siteId=291194637