Home >Java >javaTutorial >How to use java's short-circuit logical operator
Explanation
1. Logical operators perform short-circuit evaluation.
2. The so-called short circuit means that when one operand participating in the operation is enough to infer the value of the expression, the other operand (possibly an expression) will not be executed.
When using logical operators, when both operands are true, the result is true, but when the first operation is false, the result must be false, and the second one is no longer judged. operate.
Example
public static void main(String[] args) { int a = 5;//定义一个变量; boolean b = (a < 4) && (a++ < 10); //使用短路逻辑运算符的结果为false System.out.println("使用短路逻辑运算符的结果为" + b); //a的结果为5 System.out.println("a的结果为" + a); }
This program uses the short-circuit logical operator (&&). First, it is judged that the result of a<4 is false, and the result of b is false, so it is not Then perform the second operation to judge a <10, so the value of a is 5.
Java is an object-oriented programming language that can write desktop applications, Web applications, distributed systems and embedded system applications.
The above is the detailed content of How to use java's short-circuit logical operator. For more information, please follow other related articles on the PHP Chinese website!