Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean range in Java programming language

I found the below paragraph in SCJP 6.0 book. What does it mean by the last statement.Which book to read about how these variables are actually stored in memory?. Thanks a lot.

"That's virtual-machine dependent."

For boolean types there is not a range; a boolean can be only true or false. If someone asks you for the bit depth of a boolean, look them straight in the eye and say, "That's virtual-machine dependent." They'll be impressed.

like image 546
oortcloud_domicile Avatar asked May 24 '26 21:05

oortcloud_domicile


2 Answers

It means that the only thing required from boolean is to be true or false, no matter what is the underlying implementation.

JLS states:

The boolean type has exactly two values: true and false.

Where for integral types:

The integral types are byte, short, int, and long, whose values are 8-bit, 16-bit, 32-bit and 64-bit signed two's-complement integers, respectively, and char, whose values are 16-bit unsigned integers representing Unicode characters.

So you have required bit depth for integral types but it is solely up to you if your boolean will be represented in the memory as a single bit, byte or multibyte variable when you implement your own JVM.

like image 181
Jan Zyka Avatar answered May 27 '26 10:05

Jan Zyka


What is being said is that a VM can implement a boolean as an integer, where a positive value is true and 0 or a negative value is false. The idea is that as long as the VM handles booleans strictly as true or false, it doesn't matter how the VM implements booleans at the OS level.

like image 36
Jeremy Avatar answered May 27 '26 11:05

Jeremy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!