JVM 中“boolean”类型的用途是什么?

What is the purpose of `boolean` type in JVM?

JVMS8said:

Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.

的确,这两个方法:

boolean expr1(boolean a, boolean b) {
    return a || b;
}

int expr2(int a, int b) {
    return ((a != 0) || (b != 0)) ? 1 : 0;
}

将生成相同的字节码(方法签名除外)

  boolean expr1(boolean, boolean);
    Signature: (ZZ)Z
    Code:
       0: iload_1       
       1: ifne          8
       4: iload_2       
       5: ifeq          12
       8: iconst_1      
       9: goto          13
      12: iconst_0      
      13: ireturn       

  int expr2(int, int);
    Signature: (II)I
    Code:
       0: iload_1       
       1: ifne          8
       4: iload_2       
       5: ifeq          12
       8: iconst_1      
       9: goto          13
      12: iconst_0      
      13: ireturn       

所以,我不明白为什么JVM还需要boolean打字?仅用于方法签名的运行时检查?

维护方法重载至少需要它。假设我们在同一个 class

中有两个方法

boolean a(boolean x) {...}

boolean a(int x) {...}

它们可以有不同的内部逻辑,所以在字节码中应该通过它们的签名来区分它们。