What is the purpose of `boolean` type in JVM?
Asked Answered
R

2

8

As it said in JVMS8:

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.

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 the method signatures)

  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 JVM needs boolean type. Is it just for the runtime check of method signatures?

Resist answered 29/12, 2015 at 11:28 Comment(5)
You can write much cleaner code with a boolean type than with an int that's emulating a boolean.Cephalometer
@MickMnemonic Sure. But I'm talking not about java language itself, but about JVM which doesn't know about java language anything.Resist
@sidgate The question is not about using 0/1 instead. The question is about, why does JVM need to declare special kind of type boolean, not just int, while it hasn't special instructions for int. So question not about java specs, but about JVM architecture.Resist
This might be implementation specific. Even if the Oracle JVM implements booleans as ints, not all JVM implementations have to. int and boolean are semantically different, so it may be useful for a JVM implementation to be aware of the difference and use this information e.g. for optimizations.Cephalometer
@MickMnemonic I can not agree with you. This is not about specific Oracle JVM implementation, but about JVM specs: there are not special boolean instructions in JVM, so it can not be Oracle specificResist
R
10

As minimum it is needed to support methods overloading. Say, we have two methods in the same class

boolean a(boolean x) {...}

and

boolean a(int x) {...}

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

Resist answered 29/12, 2015 at 11:55 Comment(2)
The return type is part of the signature for the JVM as well.Varicella
As a simple practical example, System.out.println(booleanVar); does something different to System.out.println(intVar);Lottie
B
0

As you can see from your examples, boolean computation is all integer computation. Boolean expressions are compiled into branching (eg. ifne) operations that result in either a 0 int or 1 int on the operand stack. Once the high-level language boolean is compiled, there isn't any way to determine if the original value was a boolean because its translated into integer computation operations.

At a high-level, there are definitely advantages for a language like Java to distinguish between integer and boolean types such as method overloading. But from inspecting method instructions you will not see any advantage.

My guess is that the JVM boolean operand type is only for compiled method signatures. Even the implementations for instructions like baload and bastore pop the 32 bit integer from the operand stack and truncate to 8 bits.

Breathless answered 19/6 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.