Byte is more efficient than boolean [8]

If I need 8 variables of type boolean in a class, does Java really put them all in one byte? Or will he use a byte for each? In other words, the size of the memory depends on:

boolean a;
boolean b;
boolean c;
boolean d;
boolean e;
boolean f;
boolean g;
boolean h;

vs.

public static final int a = 0x01;
public static final int b = 0x02;
public static final int c = 0x04;
public static final int d = 0x08;
public static final int e = 0x10;
public static final int f = 0x20;
public static final int g = 0x40;
public static final int h = 0x80;
byte flags;

I ask because I will create many of these objects. Thus, getting 1 byte instead of 8 bytes of memory will be a noticeable saving.

Update: . This is exactly like related questions that list the boolean value to be stored in int (thanks for these links and wish I could find them before asking). This question is slightly different in that it represents a specific alternative to using bytes and bit flags. I do not know if this is enough so that this question is not duplicated.

2: , SizeofUtil . 8 24 / 3 /. 10 /. , 8, int ( 64- ). ?

+4
4

" ?" . BitSet , byte , , 8 .

: 8 .

+3

, Java, : 1 true, 0 false. Java.

: true false. , / . , "" , .

, byte, .

+7

VM, .

, , , - ( int).

, (, 10M) , (, API Runtime).

, . , , ( , ).

0

If you look at the SpliteratorJDK 8, they are still using bit masks, and the only reason I can think about memory efficiency is:

 public static final int DISTINCT   = 0x00000001;
 public static final int SORTED     = 0x00000004;
 public static final int ORDERED    = 0x00000010;
 public static final int SIZED      = 0x00000040;

So this should be a great approach.

0
source

All Articles