java 各基本数据类型 所占有的字节数

 

Int: 4 字节
Short: 2字节
Long: 8字节
Byte: 1字节
Character: 2字节
Float: 4字节
Double: 8字节

Boolean:系统没有提供Size方法;

 
  1. public class Calcsizof {

  2.  
  3. /**

  4. * @param args

  5. */

  6. private static void calSize() {

  7. System.out.println("Integer: " + Integer.SIZE/8); // 4

  8. System.out.println("Short: " + Short.SIZE/8); // 2

  9. System.out.println("Long: " + Long.SIZE/8); // 8

  10. System.out.println("Byte: " + Byte.SIZE/8); // 1

  11. System.out.println("Character: " + Character.SIZE/8); // 2

  12. System.out.println("Float: " + Float.SIZE/8); // 4

  13. System.out.println("Double: " + Double.SIZE/8); // 8

  14. System.out.println("Boolean: " + Boolean.toString(false));

  15.  
  16. }

  17. public static void main(String[] args) {

  18. // TODO Auto-generated method stub

  19. Calcsizof calcsizof= new Calcsizof();

  20. Calcsizof.calSize();

  21. }

  22. }

运行结果:console 输出

Integer: 4
Short: 2
Long: 8
Byte: 1
Character: 2
Float: 4
Double: 8
Boolean: false

更多地封装后的Object的占用空间的分析 请参考

猜你喜欢

转载自blog.csdn.net/qq_38384440/article/details/81776320