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 much memory takes a boolean?

 
Peter Heide
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A boolean variable can be true or false.

Theoretically it fits in one single bit of memory which can contain 0 or 1.
I am talking about the contents of a boolean, not about the reference.

But how much memory will be exact taken in Java when you write something like:



In the book Head First Java 2nd Edition by Kathy Sierra & Bert Bates from O'Reilly ISBN 0596009208 in 2005 on Page 51 they say:

boolean Bit Depth (JVM-specific)

In the ExamScam Sun Certified Java Associate Mock Exam Questions by Cameron McKenzie from 2006 with ISBN 1-59872-642-0 on Answer 14-1 I read:

boolean only consumes one bit of data

I also have a German Java 5 Programmierhandbuch from Ulrike Boettcher and Dirk Frischalowski ISBN 3-935042-63-9 from 2005 where I read on Page 62:

Boolean Groesse (which is Size) 1 Byte
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know, the size of a Java boolean is not specified, and this leaves it JVM-specific, as indicated by K&B. The most detail I've found on this point is under JVM Specification - 3.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.

So although 1 bit might seem logical, I don't know that there's any foundation for saying that's the size of a Java boolean.

The bottom line: You should not be asked this on the SCJA exam.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A boolean is a true or false value, and can be stored in a lonely, single bit of memory. BUT, as Marc noted, the exact requirements are not outlined in the spec, and some operating systems cannot write to a bit, but minimally, to a byte, so a byte might end up being used by default, even thout only a bit is required.

Some JVMs actually write a boolean bit to a byte of memory. A second bit gets written into the same byte, and so on, and so on, until the entire byte is consumed.

So, a boolean value, can be represented by a single bit of memory. How much memory a JVM actualy uses is a different story, and fortunately, JVM intricacies are not tested on the SCJA exam.

From the Sun Tutorials


boolean: The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.


By the way, you're not the only one to cut me some flack for throwing that one in there.

-Cameron McKenzie

[ January 12, 2007: Message edited by: Cameron W. McKenzie ]
[ January 12, 2007: Message edited by: Cameron W. McKenzie ]
 
Peter Heide
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Marc and Cameron,
thank you for answering my question so good and detailled!
Peter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic