This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes size for data type boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "size for data type boolean" Watch "size for data type boolean" New topic
Author

size for data type boolean

Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
Please let me know how much memory size does a boolean type take?


Sanjeev Kaushik
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
That depends on the JVM.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Sanjeev Kaushik
Ranch Hand

Joined: Aug 01, 2002
Posts: 105
But there must be some range.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
The size of all other primitives in Java are well-defined, but the size of boolean is not. I not aware of any JVM suppliers documenting how they handle booleans. I think it's safe to say that it's most likely between 1 and 32 bits in length, though that probably doesn't help you much. In fact it's probably between 1 and 8 bits: since all JVMs will support the "byte" data type, we know they can reference memory to that granularity.
The fact is, it's not defined. And there is no "sizeof" operator to fall back on ...
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
The size of all other primitives in Java are well-defined, but the size of boolean is not. I not aware of any JVM suppliers documenting how they handle booleans. I think it's safe to say that it's most likely between 1 and 32 bits in length, though that probably doesn't help you much. In fact it's probably between 1 and 8 bits: since all JVMs will support the "byte" data type, we know they can reference memory to that granularity.
The fact is, it's not defined. And there is no "sizeof" operator to fall back on ...
P. Sagdeo
Ranch Hand

Joined: Nov 13, 2003
Posts: 67
what i've heard is that boolean usually takes up 1 byte
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8876
    
    8

Originally posted by Parth S.:
what i've heard is that boolean usually takes up 1 byte

You are correct, at least as far as the output of a java.util.RandomAccessFile on Windows 2000 using JDK 1.4.2_01. However, in memory the operating system can't work with a discrete piece of information smaller than a word, which in Win2k is 4 bytes. You can use java.util.BitSet to pack a bunch of booleans together, but it seems silly to worry about the storage of a couple of booleans when any non-trivial program will have hundreds of String objects floating around.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: size for data type boolean