1. public class MainTest {
    2. public static void main(String args[]) throws Exception {
    3. System.out.println("int占的字节:"+Integer.BYTES);
    4. System.out.println("long占的字节:"+Long.BYTES);
    5. System.out.println("short占的字节:"+Short.BYTES);
    6. System.out.println("char占的字节:"+Character.SIZE/8);
    7. System.out.println("float占的字节:"+Float.SIZE/8);
    8. System.out.println("double占的字节:"+Double.SIZE/8);
    9. }
    10. }

    输出:

    1. int占的字节:4
    2. long占的字节:8
    3. short占的字节:2
    4. char占的字节:2
    5. float占的字节:4
    6. double占的字节:8

    注意.BYTES返回的是字节数byte,而.SIZE返回的是比特数bit,所以要除以8