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."
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.
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 .
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.
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.