• java boolean占用内存是多少


    一,结论

    关于boolean占用内存是多少,我在JVM规范中找到以下解释,但是怎么验证呢?

    虚拟机没有给boolean(布尔)类型设置单独指令。boolean型的数据是有integer指令,包括interger返回来处理的。boolean型数组则是用byte数组来处理的。

    二,验证

    写了两个简单的程序,使用jclasslib查看字节码,对照Java虚拟机指令集后验证结论正确。

     Chapter 6. The Java Virtual Machine Instruction Set

    1,boolean型的数据=>32位

    代码

    1. public class Test1Type {
    2. public static void main(String[] args) {
    3. boolean a=true;
    4. byte b=1;
    5. System.out.println(a);
    6. System.out.println(b);
    7. }
    8. }

    对照图

    2,boolean数组型=>8位

    代码

    1. public class Test1Type {
    2. public static void main(String[] args) {
    3. boolean[] a=new boolean[]{true};
    4. byte[] b=new byte[]{1};
    5. System.out.println(a[0]);
    6. System.out.println(b[0]);
    7. }
    8. }

    对照图

    三,查看字节码

    如果没有安装jclasslib可以使用javap

    javap -v Test1Type.class

    输出结果:

    1. D:\Program Files\JetBrains\idea18\work\nacos-develop\demo\core\target\test-classes>javap -v Test1Type.class
    2. Classfile /D:/Program Files/JetBrains/idea18/work/nacos-develop/demo/core/target/test-classes/Test1Type.class
    3. Last modified 2023-9-18; size 589 bytes
    4. MD5 checksum dafc695ce0954b2a996cc96f815ba6f3
    5. Compiled from "Test1Type.java"
    6. public class Test1Type
    7. minor version: 0
    8. major version: 52
    9. flags: ACC_PUBLIC, ACC_SUPER
    10. Constant pool:
    11. #1 = Methodref #6.#24 // java/lang/Object."":()V
    12. #2 = Fieldref #25.#26 // java/lang/System.out:Ljava/io/PrintStream;
    13. #3 = Methodref #27.#28 // java/io/PrintStream.println:(Z)V
    14. #4 = Methodref #27.#29 // java/io/PrintStream.println:(I)V
    15. #5 = Class #30 // Test1Type
    16. #6 = Class #31 // java/lang/Object
    17. #7 = Utf8
    18. #8 = Utf8 ()V
    19. #9 = Utf8 Code
    20. #10 = Utf8 LineNumberTable
    21. #11 = Utf8 LocalVariableTable
    22. #12 = Utf8 this
    23. #13 = Utf8 LTest1Type;
    24. #14 = Utf8 main
    25. #15 = Utf8 ([Ljava/lang/String;)V
    26. #16 = Utf8 args
    27. #17 = Utf8 [Ljava/lang/String;
    28. #18 = Utf8 a
    29. #19 = Utf8 [Z
    30. #20 = Utf8 b
    31. #21 = Utf8 [B
    32. #22 = Utf8 SourceFile
    33. #23 = Utf8 Test1Type.java
    34. #24 = NameAndType #7:#8 // "":()V
    35. #25 = Class #32 // java/lang/System
    36. #26 = NameAndType #33:#34 // out:Ljava/io/PrintStream;
    37. #27 = Class #35 // java/io/PrintStream
    38. #28 = NameAndType #36:#37 // println:(Z)V
    39. #29 = NameAndType #36:#38 // println:(I)V
    40. #30 = Utf8 Test1Type
    41. #31 = Utf8 java/lang/Object
    42. #32 = Utf8 java/lang/System
    43. #33 = Utf8 out
    44. #34 = Utf8 Ljava/io/PrintStream;
    45. #35 = Utf8 java/io/PrintStream
    46. #36 = Utf8 println
    47. #37 = Utf8 (Z)V
    48. #38 = Utf8 (I)V
    49. {
    50. public Test1Type();
    51. descriptor: ()V
    52. flags: ACC_PUBLIC
    53. Code:
    54. stack=1, locals=1, args_size=1
    55. 0: aload_0
    56. 1: invokespecial #1 // Method java/lang/Object."":()V
    57. 4: return
    58. LineNumberTable:
    59. line 1: 0
    60. LocalVariableTable:
    61. Start Length Slot Name Signature
    62. 0 5 0 this LTest1Type;
    63. public static void main(java.lang.String[]);
    64. descriptor: ([Ljava/lang/String;)V
    65. flags: ACC_PUBLIC, ACC_STATIC
    66. Code:
    67. stack=4, locals=3, args_size=1
    68. 0: iconst_1
    69. 1: newarray boolean
    70. 3: dup
    71. 4: iconst_0
    72. 5: iconst_1
    73. 6: bastore
    74. 7: astore_1
    75. 8: iconst_1
    76. 9: newarray byte
    77. 11: dup
    78. 12: iconst_0
    79. 13: iconst_1
    80. 14: bastore
    81. 15: astore_2
    82. 16: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
    83. 19: aload_1
    84. 20: iconst_0
    85. 21: baload
    86. 22: invokevirtual #3 // Method java/io/PrintStream.println:(Z)V
    87. 25: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;
    88. 28: aload_2
    89. 29: iconst_0
    90. 30: baload
    91. 31: invokevirtual #4 // Method java/io/PrintStream.println:(I)V
    92. 34: return
    93. LineNumberTable:
    94. line 5: 0
    95. line 6: 8
    96. line 8: 16
    97. line 9: 25
    98. line 10: 34
    99. LocalVariableTable:
    100. Start Length Slot Name Signature
    101. 0 35 0 args [Ljava/lang/String;
    102. 8 27 1 a [Z
    103. 16 19 2 b [B
    104. }
    105. SourceFile: "Test1Type.java"

  • 相关阅读:
    elasticsearch7.17开发笔记
    介绍rabbitMQ
    想要精通算法和SQL的成长之路 - 旋转链表
    C++面试题(基础)
    Pyside6 安装和简单界面开发
    CSS语法
    C++ vector容器
    monai如何读取.nii/.nii.gz数据
    Python还是很迷茫的小伙伴进来,教你用图秒懂Python
    发布Android库至MavenCentral详解
  • 原文地址:https://blog.csdn.net/qq_39308071/article/details/132988975