java - 带有 && 的 If 语句,其中第一个条件需要在测试第二个条件之前为真

标签 java if-statement conditional-statements

<分区>

抱歉,如果这是重复的,但我什至想不出搜索它的方法。我不确定要问这个问题的术语,所以我将解决我的问题。

根据我的测试,似乎 if 语句会在尝试转到数组 [10] 之前退出。我总是这样吗?意思是,如果我的 if 语句中有一个 && 并且左侧为假,它是否总是会在测试第二个之前退出?就此而言,它会始终先测试左侧吗?

public static void main(String[] args) {
        boolean [] array = new boolean [10];
        Arrays.fill(array, true);
        int i = 10;
        if(i < array.length && array[i]){
            System.out.println("WhoHoo!");
        }
}

最佳答案

来自Java Tutorials关于运营商:

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

关于java - 带有 && 的 If 语句,其中第一个条件需要在测试第二个条件之前为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16033085/

相关文章:

java - 在 Android 上检索设备信息

c++ - 创建一个可以是多种类型的 C++ 数组

根据 r 数据框中另一个分类变量的值重新编码分类变量

c# - C#中合并嵌套If(C#中的短路关键字)

r - 将数据集分成 2 个较小的数据集

java - 字符集未正确应用

java - Android SQLite 从空表中获取列列表

java - Maven Jaxb 插件无法从目标文件夹生成类

string - Excel VBA 指令 if 语句 false

Python:在满足特定条件时跳过代码块(计算)而不使用 "if"语句