📜  Java中的短路逻辑运算符与示例

📅  最后修改于: 2022-05-13 01:54:46.482000             🧑  作者: Mango

Java中的短路逻辑运算符与示例

在Java逻辑运算符中,如果逻辑表达式的求值在完成求值之前退出,则称为短路。发生短路是因为即使在表达式的完整计算之前结果就很清楚了,并且返回了结果。短路评估避免了不必要的工作并导致高效处理。

以下是Java中发生的各种类型的短路:

1.AND(&&)短路:

在 AND 的情况下,表达式会被计算,直到我们得到一个错误的结果,因为结果总是错误的,与其他条件无关。如果存在带有 &&(逻辑与) 的表达式,并且第一个操作数本身为假,则发生短路,不计算进一步的表达式,并返回假。

示例:使用 AND(&&)运算符进行短路。

Java
// Java code to demonstrate the
// short circuiting using &&
  
import java.io.*;
  
class ShortCirAND {
    public static void main(String arg[])
    {
  
        // Since first operand is false
        // and operator is &&,
        // Evaluation stops and
        // false is returned.
        if (false && true && true) {
            System.out.println("This output "
                               + "will not "
                               + "be printed");
        }
        else {
  
            System.out.println("This output "
                               + "got printed actually, "
                               + " due to short circuit");
        }
  
        // Whole expression will be evaluated,
        // as no false is encountered
        // before last condition
        // Therefore no Short circuit
        if (true && true && true) {
            System.out.println("This output "
                               + "gets print"
                               + " as there will be"
                               + " no Short circuit");
        }
        else {
  
            System.out.println("This output "
                               + "will not "
                               + "be printed");
        }
    }
}


Java
// Java program to demonstrate the
// short circuiting using OR
  
class ShortCirOR {
    public static void main(String arg[])
    {
  
        // Since first operand is true
        // and operator is ||,
        // Evaluation stops and
        // true is returned.
        if (true || false || false) {
            System.out.println("This output "
                               + "got printed actually, "
                               + " due to short circuit");
        }
        else {
            System.out.println("This output "
                               + "will not "
                               + "be printed");
        }
  
        // Whole expression will be evaluated,
        // as no true is encountered
        // before last condition
        // Therefore no Short circuit
        if (false || false || true) {
            System.out.println("This output "
                               + "gets print"
                               + " as there will be"
                               + " no Short circuit");
        }
        else {
  
            System.out.println("This output "
                               + "will not "
                               + "be printed");
        }
    }
}


输出
This output got printed actually,  due to short circuit
This output gets print as there will be no Short circuit

2. OR(||) 短路:

在 OR 的情况下,表达式会被计算,直到我们得到一个真结果,因为结果将永远为真,与其他条件无关。如果存在带有 ||(逻辑 OR) 的表达式,并且第一个操作数本身为真,则会发生短路,计算停止,并返回真。

示例:使用 OR(||) 进行短路。

Java

// Java program to demonstrate the
// short circuiting using OR
  
class ShortCirOR {
    public static void main(String arg[])
    {
  
        // Since first operand is true
        // and operator is ||,
        // Evaluation stops and
        // true is returned.
        if (true || false || false) {
            System.out.println("This output "
                               + "got printed actually, "
                               + " due to short circuit");
        }
        else {
            System.out.println("This output "
                               + "will not "
                               + "be printed");
        }
  
        // Whole expression will be evaluated,
        // as no true is encountered
        // before last condition
        // Therefore no Short circuit
        if (false || false || true) {
            System.out.println("This output "
                               + "gets print"
                               + " as there will be"
                               + " no Short circuit");
        }
        else {
  
            System.out.println("This output "
                               + "will not "
                               + "be printed");
        }
    }
}
输出
This output got printed actually,  due to short circuit
This output gets print as there will be no Short circuit