• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

size for data type boolean

 
Sanjeev Kaushik
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please let me know how much memory size does a boolean type take?
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on the JVM.
 
Sanjeev Kaushik
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there must be some range.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i've heard is that boolean usually takes up 1 byte
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic