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

As he said in JVMS8 :

Although the Java virtual machine defines a boolean type, it provides only very limited support. The Java Virtual Machine instructions are not specifically dedicated to boolean operations. Instead of this expression in Java, a programming language that works with Boolean values ​​is compiled to use data types of the Java virtual machine.

Indeed, these two methods:

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

will produce the same byte code (except for the method signature)

  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 

So, I do not understand why the JVM needs a boolean type yet? Just for checking the runtime of method signatures?

+5
source share
1 answer

At a minimum, this is necessary to overload the maintenance method. Say we have two methods in one class

boolean a(boolean x) {...}

and

boolean a(int x) {...}

They can have different internal logic, so in byte code they should be distinguished by their signatures.

+5
source

Source: https://habr.com/ru/post/1239420/


All Articles