wood burning stoves
The moose likes Beginning Java and the fly likes Size of boolean . Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Size of boolean ." Watch "Size of boolean ." New topic

Size of boolean .

vaibhav panghal

Joined: Feb 24, 2008
Posts: 25
hi all ,
now i'm a little cofused over this one . what's the size of boolean in memory?
i know that it's JVM dependent but then why some people say that it takes 1 byte while others say that it doesn't take any memory at all ? i'll also quote something from just java 2 here .


This is the data type used for true/false conditions. To speed up memory access, implementations don't pack boolean values into the theoretical one-bit minimum space, but put each boolean into a byte.
Unlike some other languages, the Java boolean type is not based on integers. In particular, the Java programmer cannot increment, decrement, shift, or add boolean values. Inside a JVM, however, there are no instructions specifically for booleans, so integer operations (like assigning zero) are used. In the Sun JVM, the byte containing a boolean is promoted to 32 bits when pushed onto the stack during execution. So don't think booleans are a 1-bit type that economizes on storage."

what is the author trying to say ?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

He's saying that booleans are mostly stored as ints. It's possible that in some JVM implementations, an array of boolean might be stored as an array of bytes or even a bit field, but there's really no way to know other than by looking at the source code for the JVM you're using.

He should also be saying don't worry about it. This kind of behind-the-scenes information is rarely useful, especially to the Java beginner.

[Jess in Action][AskingGoodQuestions]
vaibhav panghal

Joined: Feb 24, 2008
Posts: 25
yes i know that i don't have to _worry_ about it but doesn't hurt to know stuff , you would agree . one more thing ( and i've read it in many places like the sun's developer forums ) - why is it that an array of boolean values are stored as bytes ( one byte for each boolean value ) and otherwise the memory it takes is JVM dependent ? thanking in anticipation . Vaib .
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Because Sun thought that would be a good idea, when balancing performance (which suggests using whole words for booleans) against storage requirements (which suggests using the smallest possible booleans).

A good thing about Java is that you are shielded from the differences between different platforms and JVMs. It is incredibly rare to need to know how big any particular data item is, in Java.

On the extremely rare occasions when you do need to know, there's no programmatic way to find out. You may get an answer from experiment or documentation, but it will be specific to one JVM implementation on one platform.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Originally posted by vaibhav panghal:
yes i know that i don't have to _worry_ about it but doesn't hurt to know stuff , you would agree .
I'm not sure I agree with that. 10 years ago there were a lot of surveys that showed that Java was "slower" than some other languages. There are still a lot of people who "know" that information and base decisions on it. What I'm saying here is that there is useful information and there is useless information and there is information that appears to be useful but actually isn't.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
It's more important that you understand the possible ways in which booleans might be implemented - i.e. the basics of computer science. And know that the JVM can decide itself how to implement them.

That the architecture of the byte-code (the same for all JVMs) might direct most implementer in a certain direction.

That there is no way to programmatically ensure the implementation, and that you should therefor not depend on an implementation.

And that if fiddling bits is an important aspect of the program you need to write, then Java is possibly a bad candidate for your solution.

and that there is always the next thing that you don't or can't know - figure this one out, and it will come along, �

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
vaibhav panghal

Joined: Feb 24, 2008
Posts: 25
thank you so much all of you . i think i got the concept from the discussion here .
I agree. Here's the link: http://aspose.com/file-tools
subject: Size of boolean .
It's not a secret anymore!