Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How much space does boolean takes up in Java ?

 
Shunjie Liu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I know that an int takes up 32 bits but what about boolean ? does it takes up 2 bits in the heap ? Is it the same as Boolean Object?

Thank you ^^
[ March 27, 2006: Message edited by: Shunjie Liu ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that is left up to the specific JVM to decide. I'm not sure.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS does not specify the size of a boolean, it simply specifies that it has two values: true and false. Whether or not those values are stored as a single bit or 64 bits is entirely up to the JVM.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, you should not worry about exactly how much space is used by fields and objects in Java. It isn't fixed and the language doesn't let you find out (well, maybe there's something in 1.5...?).

A big reason for Java's popularity is that it gets you away from worrying about low-level stuff like how the data is actually stored in memory.

Occasionally, you do care, because you are working with huge amounts of data in memory, and want to avoid using more than you have to. A little experimentation is required, then, but remember that any results will be specific to the JVM and platform you're testing on.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shunjie Liu:
Hi guys,
I know that an int takes up 32 bits but what about boolean ? does it takes up 2 bits in the heap ? Is it the same as Boolean Object?


That would be a two bit implementation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic