1

This is a very simple and maybe a worthless question: Whitch one of the following two consumes more memory?

boolean[][] var = new boolean[32768][32768];

or

byte[][] var = new byte[32768][32768];

I heard rumors that java stores every piece of data in it's own byte in both cases. Then whitch one is more efficent? Is it possible to somehow treat a long value as a boolean array?

Gergely
  • 159
  • 2
  • 14
  • Where did you hear those rumors? – Tunaki Mar 19 '16 at 17:45
  • 2
    I'm voting to close this question as off-topic because this is an implementation detail and is trivially answered with a simple experiment. – Jim Garrison Mar 19 '16 at 17:47
  • Check this: http://stackoverflow.com/a/1907455/971067 – rdonuk Mar 19 '16 at 17:48
  • You really should not worry about performance when using primitive types. The only thing to remember is that when you are dealing with **large** sets of data is that the reference type brothers of primitive types (Byte, Integer, ...) are definitely more expensive in terms of memory. But unless you are dealing 10s or 100s of thousands of items; you really do not need to care. – GhostCat Mar 19 '16 at 17:56

1 Answers1

4

From Primitive Data Types

The boolean data type has only two possible values: true and false. Use this data type for simple flags that track true/false conditions. This data type represents one bit of information, but its "size" isn't something that's precisely defined.

However, in the Oracle JVM it uses 1 byte per bit so the memory size and efficiency is the same.

If you would like to use 1 bit per bit, I suggest using a BitSet.

Is it possible to somehow treat a long value as a boolean array?

yes, although I can't image why you would want to.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130