Home >Java >javaTutorial >How to use question mark and colon in Java? :expression
This article will introduce to you the usage of question mark and colon "?:" expressions in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Let’s start with a piece of Java code:
return data != null ? data.getName() : null;
Okay! =The following code is divided into three parts: A, B, and C
A is a Boolean expression (true/false), B and C are execution statements.
The most intuitive: A? B: C, which means that if A is true, execute B, otherwise execute C
The above code means that if data is not empty, return data.getName(), if data is empty, return null.
Extension
After reading the above explanation, let’s extend it again
2 ==0 ? 1 1: 2-2;
Such as 2 Equal to 0, execute 1 1. If 2 is not equal to 0, execute 2-2.
Recommendation: "java Video Tutorial"
The above is the detailed content of How to use question mark and colon in Java? :expression. For more information, please follow other related articles on the PHP Chinese website!