Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How Many bit/bytes does Boolean takes?

 
Suresh Regmi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just confused on the size that Boolean data type takes, is it 1bit/Byte or just True/False. I believe there should be something in terms of bit/byte.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want to know about the boolean primitive data type (with lower case b) or the Boolean object (with upper case B)? Or both?

And why do you want to know? That's definitely not on the certification exams (as it is probably virtual machine dependent).
 
Paweł Baczyński
Bartender
Posts: 1877
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have checked this in JVLS and it says:
JVLS wrote: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.

I also compiled this code and executed javap -verbose on it:
The result was:
That being said, I doubt you will ever need this information. And you will definitely not need it for any Oracle certification exam.
 
Campbell Ritchie
Sheriff
Posts: 51390
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I remember, the Java® Language Specification defines the number of bits used for all primitives, except boolean, which is left undefined.
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say the answer is: it doesn't matter.
 
Suresh Regmi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel, for both with small b and Capital B.
 
Simon Roberts
Author
Ranch Hand
Posts: 170
7
Java Linux Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I believe you'll find that the spec does _not_ specify the storage size of anything. Instead, it describes how the data behaves. You cannot represent an int, for example in less than 32 bits, but you can use more, and waste them.

Indeed, I believe that a method local boolean is typically stored on the stack as a machine word (probably 32 bits) for alignment reasons. However, at one time, at least, arrays of booleans were stored as packed bits.

But, as has been said, the exam will never question this, precisely because storage is not defined by the requirements. Only effective behavior is.

 
Campbell Ritchie
Sheriff
Posts: 51390
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am 99% sure that in older versions (maybe 1.0 to 1.3) a short was stored in 32 bits even though its data representation is 16 bits. So they were happy to waste storage when storage was several orders of magnitude more expensive than now.
 
Simon Roberts
Author
Ranch Hand
Posts: 170
7
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FWIW, I just took a look and the JSL talks about the "size" of values, but not of storage. It describes the _values_ as being e.g. 32bit twos complement, but nothing is said about storage. Which makes sense in a platform independent language. As to boolean it says no such thing; only that it has two values true and false.

On the 32bits for a short point, the JVM has always been at liberty to do what it needs to to suit a given piece of implementation hardware in this respect, and some hardware cannot easily address less than 32 bits in a read/write operation, so for those, performance could be gained at the expense of memory efficiency in just the way you describe. I have a feeling that was more the case with Intel hardware than Sparc, but memory is fading
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh Regmi wrote:Roel, for both with small b and Capital B.

I still wonder why as it's not required for any certification exam at all. And Java is a platform independent language, so storage wise a boolean on a Windows 64-bit machine might be different from a boolean on a Mac OS X machine...
 
Campbell Ritchie
Sheriff
Posts: 51390
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simon Roberts is right. If you read the Java® Language Specification (=JLS), this is what it says
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 UTF-16 code units (§3.1).

The floating-point types are float, whose values include the 32-bit IEEE 754 floating-point numbers, and double, whose values include the 64-bit IEEE 754 floating-point numbers.

The boolean type has exactly two values: true and false.
It does not say anything about how much storage is used for a double, but says that doubles include the IEEE 754 64 bit foloating‑point numbers. Similarly it says nothing about storage for a boolean.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Java is a platform independent language, so storage wise a boolean on a Windows 64-bit machine might be different from a boolean on a Mac OS X machine...


I haven't understood this.Being platform independent doesn't means independent of os and processor so how will they be different in both cases?
 
Campbell Ritchie
Sheriff
Posts: 51390
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote: . . . Being platform independent doesn't means independent of os and processor . . .
But it does mean independence of processor and OS.

Remember Java® was originally developed in the mid‑1990s when there were many 16‑bit machines around; 64‑bit computers had never been heard of then. If the JRE hs changed to use 8 bytes for an int on a 64‑bit computer, why should we worry about that.
 
Tim Holloway
Saloon Keeper
Posts: 18362
56
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You guys are vastly underestimating how evil language implementors can be if you assume that the storage occupancy of a primitive or object is something that has a fixed value.

Simpl;y example: instead of allocating a byte (minimum addressable RAM unit) for a boolean, one could allocate a byte/byte array to contain a whole set of booleans, one per bit and reference them with a bit offset value. But on the stack, a boolean might be contained in a 32 or 64-bit word for optimal hardware user. Speaking of which, some CPU architectures can address bits directly, others require extra logic to pry them out. Which can lead to trade-offs between efficient memory access version access time. And we're dealing with a JIT re-optimizing compiler here, so it's even possible that the JVM could switch between mechanisms on the fly.

Old-time IBM mainframes (System/360) had different sized hardware data buses, depending on the model, so even though they all had the same instruction set, some models were optimal with 16-bit objects on even memory boundaries, some were optimal with 32-bit objects on 32-bit memory boundaries, and some were even capable of fetching 32-bit objects that were 16-bit aligned efficiently. More recent CPUs, of course, have multi-level cache and pipelining to factor in.

Tricks like these - and even more arcane ones - are also the reason that disk storage requirements for DBMS data types tend to be weaselly or non-existent these days. It's not like the old times, when, for example, you could manually and trivially compute to the byte how much storage every field in a dBase III table was going to take up.

In other words, how much RAM does a Java boolean require? As much as it needs to.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry CR
This auto correct thing in my mobile is killing me I typed does and god knows how it managed to type doesn't
 
Campbell Ritchie
Sheriff
Posts: 51390
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Predictive text is a law unto itself. Nobody can get it to work.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic