Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a boolean data type work internally in Java? [duplicate]

Tags:

java

boolean

When you assign the value "true" to a boolean data type in Java (e.g. boolean variableName = true;), what value is actually being assigned to that variable?

like image 487
McGrizz Avatar asked Sep 18 '25 23:09

McGrizz


2 Answers

The Java Virtual Machine Specification states in section 2.3.4:

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.

. . .

The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.

like image 148
David Conrad Avatar answered Sep 21 '25 16:09

David Conrad


The actual value assigned to the variable is true.

But internally, in the virtual machine, booleans may be represented by ints.

From the Java Virtual Machine Specification, section 2.3.4, The boolean type:

The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.

like image 20
Andy Thomas Avatar answered Sep 21 '25 18:09

Andy Thomas