In Java, ** boolean variables are not 1 byte! (Conclusion) **
... maybe. (Boso Let's check it.
Just give me the conclusion! The human being Please skip to "at the end".
Write the following code:
Sandbox.java
public class Sandbox{
public static void main(String[] args){
boolean b = true;
//Because of the StackMapTable attribute
if(b){
b=true;
}
}
}
Compile this as usual and look at the contents of the output ".class" file.
> javac Sandbox.java
> javap -v Sandbox
(Omission)
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: (0x0009) ACC_PUBLIC, ACC_STATIC
Code:
stack=1, locals=2, args_size=1
0: iconst_1
1: istore_1
2: iload_1
3: ifeq 8
6: iconst_1
7: istore_1
8: return
LineNumberTable:
line 3: 0
line 4: 2
line 5: 6
line 7: 8
StackMapTable: number_of_entries = 1
frame_type = 252 /* append */
offset_delta = 8
locals = [ int ]
}
SourceFile: "Sandbox.java"
this
locals = [ int ]
Please pay attention to. This means that the local variable is an int. (https://docs.oracle.com/javase/specs/jvms/se14/html/jvms-4.html#jvms-4.7.4)
In other words
** Boolean variables as local variables are converted to int variables **
about it.
Write the following code.
Sandbox.java
public class Sandbox{
boolean b;
public static void main(String[] args){
}
}
Do the same as before:
> javac Sandbox.java
> javap -v Sandbox
(Omission)
{
boolean b;
descriptor: Z
flags: (0x0000)
(Omission)
}
SourceFile: "Sandbox.java"
this
descriptor: Z
That is, "the type is boolean". (https://docs.oracle.com/javase/specs/jvms/se14/html/jvms-4.html#jvms-4.3.2)
So
** A boolean variable as a class field is treated as a boolean variable **
The thing was shown. (Of course)
So what is the size of this variable? Write the following code:
Sandbox.java
public class Sandbox{
static int i;
static boolean b;
public static void main(String[] args){
b=true;
i=0x111111;
}
}
First, compile this.
> javac Sandbox.java
** Open the output ".class" file with a binary editor and ** Find the byte sequence B3 00 0E and rewrite it as B3 00 07. That is, this I will do this.
Note: There may be 0E in addition to the B3 00 0E column, but do not rewrite it.
Then, look at the contents with javap.
> javap -v Sandbox
(...Omission)
Code:
stack=1, locals=1, args_size=1
0: iconst_1
1: putstatic #7 // Field b:Z
4: ldc #13 // int 1118481
6: putstatic #7 // Field b:Z
9: return
(Omission...)
If this happens, you are successful.
Note: This operation is rewritten to assign a number to variable b instead of assigning a number to variable i.
Execute this modified version ".class".
> java Sandbox
The fact that nothing was displayed was successful. In other words, you can assign 3 bytes of data to a boolean variable,
** Boolean class fields can be operated in the same way as int variables **
about it. (https://docs.oracle.com/javase/specs/jvms/se14/html/jvms-2.html#jvms-2.11.1)
Write the following code.
Sandbox.java
public class Sandbox{
static boolean b[];
public static void main(String[] args){
boolean[] lb = new boolean[]{true};
//Because of the StackMapTable attribute
if(lb[0]){
b[0]=true;
}
}
}
Compile this and look at its contents.
>javac Sandbox.java
>javap -v Sandbox
(...Omission)
{
static boolean[] b;
descriptor: [Z
flags: (0x0008) ACC_STATIC
(...Omission...)
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: (0x0009) ACC_PUBLIC, ACC_STATIC
Code:
stack=4, locals=2, args_size=1
0: iconst_1
1: newarray boolean
3: dup
4: iconst_0
5: iconst_1
6: bastore
7: astore_1
8: aload_1
9: iconst_0
10: baload
11: ifeq 20
14: getstatic #7 // Field b:[Z
17: iconst_0
18: iconst_1
19: bastore
20: return
LineNumberTable:
line 6: 0
line 8: 8
line 9: 14
line 11: 20
StackMapTable: number_of_entries = 1
frame_type = 252 /* append */
offset_delta = 20
locals = [ class "[Z" ]
}
SourceFile: "Sandbox.java"
As before
static boolean[] b; descriptor: [Z
Means that "b is a boolean array of type". Also,
locals = [ class "[Z" ]
Means that the type of local variable is a boolean array. Therefore,
** Boolean array type variables are treated as boolean array type variables, not limited to local variables and class fields **
The thing was shown.
(Https://docs.oracle.com/javase/specs/jvms) because the boolean array itself is equivalent to a regular instance, and it's obvious that it reserves the same size (only for variables). /se14/html/jvms-2.html#jvms-2.11.5)
Investigate the size of one element. However, this was the answer. (https://docs.oracle.com/javase/specs/jvms/se14/html/jvms-2.html#jvms-2.3.4)
In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element.
That is,
** In the Oracle implementation, 8 bits (= 1 byte) are used for one element **
about it.
-** Boolean variables as local variables are converted to int variables. ** ** -** A boolean variable as a class field is treated as a boolean variable and can be operated in the same way as an int variable. ** ** -** For boolean array type variables, both local variables and class fields are treated as boolean array type variables, and the Oracle implementation uses a size of 1 byte for each element of the array. ** **
I haven't read much of the documentation, so please let me know if there are any mistakes. The following is a bonus.
By the way, the variable b used in the class field survey earlier is Using java / lang / System.out and java / io / PrintStream.println: (I) V When I try to output it, it is 1. (Note that the argument is an int).
>javap -v Sandbox
(...Omission)
static boolean b;
descriptor: Z
flags: (0x0008) ACC_STATIC
(...Omission...)
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: (0x0009) ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=1, args_size=1
0: iconst_1
1: putstatic #7 // Field b:Z
4: ldc #13 // int 1118481
6: putstatic #7 // Field b:Z
9: getstatic #18 // Field java/lang/System.out:Ljava/io/PrintStream;
12: getstatic #7 // Field b:Z
15: invokevirtual #24 // Method java/io/PrintStream.println:(I)V
18: return
(Omission...)
>java Sandbox
1
According to the putstatic documentation (https://docs.oracle.com/javase/specs/jvms/se14/html/jvms-6.html#jvms-6.5.putstatic)
If the value is of type int and the field descriptor type is boolean, then the int value is narrowed by taking the bitwise AND of value and 1, resulting in value'.
There is. Therefore, it seems that the contents of the boolean class field are guaranteed to be 0 or 1. putfield · [bastore](https://docs.oracle.com /javase/specs/jvms/se14/html/jvms-6.html#jvms-6.5.bastore) was the same.
That's why I couldn't say the size of the boolean. The actual variable size does not have to be the same as an int, as it is guaranteed to be 0 or 1. I searched the documentation but couldn't find any such description. Can anyone please tell me who is familiar with it? (Come here and throw)
that's all.