Archived

This topic is now archived and is closed to further replies.

[java] Boolean memory usage

This topic is 6393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a situation where I''m storing several boolean values in a class, and am wondering how these will be stored in memory - I realise it would depend on the VM being used, but does anyone know what the general situation is? Having looked at the VM spec all I can find is the short section on ''there is no boolean'', indicating that booleans are stored as ints, but will multiple booleans generally be put into the one int, or is it one int per boolean? Thanks Starfall

Share this post


Link to post
Share on other sites

The selection of a size for booleans is a decision for the java compiler, not the VM. The VM just uses what is given it.

I have not examined the operation of Java compilers in that much detail, but from my experience from other langauges I guess that booleans each use at least 8-bit.

If you need to store them more compactly use need to use bitwise operations to mask them in. This functionality can be encapsulated in one of your own classes if you want to.

Share this post


Link to post
Share on other sites