一,结论
关于boolean占用内存是多少,我在JVM规范中找到以下解释,但是怎么验证呢?
虚拟机没有给boolean(布尔)类型设置单独指令。boolean型的数据是有integer指令,包括interger返回来处理的。boolean型数组则是用byte数组来处理的。
二,验证
写了两个简单的程序,使用jclasslib查看字节码,对照Java虚拟机指令集后验证结论正确。
Chapter 6. The Java Virtual Machine Instruction Set
1,boolean型的数据=>32位
代码
public class Test1Type {
public static void main(String[] args) {
boolean a=true;
byte b=1;
System.out.println(a);
System.out.println(b);
}
}
对照图
2,boolean数组型=>8位
代码
public class Test1Type {
public static void main(String[] args) {
boolean[] a=new boolean[]{true};
byte[] b=new byte[]{1};
System.out.println(a[0]);
System.out.println(b[0]);
}
}
对照图
三,查看字节码
如果没有安装jclasslib可以使用javap
javap -v Test1Type.class
输出结果:
D:\Program Files\JetBrains\idea18\work\nacos-develop\demo\core\target\test-classes>javap -v Test1Type.class
Classfile /D:/Program Files/JetBrains/idea18/work/nacos-develop/demo/core/target/test-classes/Test1Type.class
Last modified 2023-9-18; size 589 bytes
MD5 checksum dafc695ce0954b2a996cc96f815ba6f3
Compiled from "Test1Type.java"
public class Test1Type
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #6.#24 // java/lang/Object."<init>":()V
#2 = Fieldref #25.#26 // java/lang/System.out:Ljava/io/PrintStream;
#3 = Methodref #27.#28 // java/io/PrintStream.println:(Z)V
#4 = Methodref #27.#29 // java/io/PrintStream.println:(I)V
#5 = Class #30 // Test1Type
#6 = Class #31 // java/lang/Object
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 LTest1Type;
#14 = Utf8 main
#15 = Utf8 ([Ljava/lang/String;)V
#16 = Utf8 args
#17 = Utf8 [Ljava/lang/String;
#18 = Utf8 a
#19 = Utf8 [Z
#20 = Utf8 b
#21 = Utf8 [B
#22 = Utf8 SourceFile
#23 = Utf8 Test1Type.java
#24 = NameAndType #7:#8 // "<init>":()V
#25 = Class #32 // java/lang/System
#26 = NameAndType #33:#34 // out:Ljava/io/PrintStream;
#27 = Class #35 // java/io/PrintStream
#28 = NameAndType #36:#37 // println:(Z)V
#29 = NameAndType #36:#38 // println:(I)V
#30 = Utf8 Test1Type
#31 = Utf8 java/lang/Object
#32 = Utf8 java/lang/System
#33 = Utf8 out
#34 = Utf8 Ljava/io/PrintStream;
#35 = Utf8 java/io/PrintStream
#36 = Utf8 println
#37 = Utf8 (Z)V
#38 = Utf8 (I)V
{
public Test1Type();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this LTest1Type;
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=4, locals=3, args_size=1
0: iconst_1
1: newarray boolean
3: dup
4: iconst_0
5: iconst_1
6: bastore
7: astore_1
8: iconst_1
9: newarray byte
11: dup
12: iconst_0
13: iconst_1
14: bastore
15: astore_2
16: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
19: aload_1
20: iconst_0
21: baload
22: invokevirtual #3 // Method java/io/PrintStream.println:(Z)V
25: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
28: aload_2
29: iconst_0
30: baload
31: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
34: return
LineNumberTable:
line 5: 0
line 6: 8
line 8: 16
line 9: 25
line 10: 34
LocalVariableTable:
Start Length Slot Name Signature
0 35 0 args [Ljava/lang/String;
8 27 1 a [Z
16 19 2 b [B
}
SourceFile: "Test1Type.java"