Home > Java > javaTutorial > body text

How to use java's short-circuit logical operator

王林
Release: 2023-05-10 17:01:06
forward
1407 people have browsed it

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);
}
Copy after login

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.

What is Java

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template