Is there any sizeof-like method in Java?

后端 未结 15 1084
暗喜
暗喜 2020-11-27 15:03

Is there any built-in method in Java to find the size of any datatype? Is there any way to find size?

15条回答
  •  青春惊慌失措
    2020-11-27 15:55

    There is a contemporary way to do that for primitives. Use BYTES of types.

    System.out.println("byte " + Byte.BYTES);
    System.out.println("char " + Character.BYTES);
    System.out.println("int " + Integer.BYTES);
    System.out.println("long " + Long.BYTES);
    System.out.println("short " + Short.BYTES);
    System.out.println("double " + Double.BYTES);
    System.out.println("float " + Float.BYTES);
    

    It results in,

    byte 1
    char 2
    int 4
    long 8
    short 2
    double 8
    float 4
    

提交回复
热议问题