java包装类数据缓存

    技术2023-12-15  80

    Java中只是对部分基本数据类型对应包装类的部分数据进行了缓存:         byte、short、int和long所对应包装类的数据缓存范围为 -128~127(包括-128和127);

    Integer b=125; Integer c=125; System.out.println(c==b);//输出true Integer b=129; Integer c=129; System.out.println(c==b);//输出false

            float和double所对应的包装类没有数据缓存范围;

    Double a=1.0; Double c=1.0; System.out.println(a==c);//输出false

            char所对应包装类的数据缓存范围为 0~127(包括0和127);

    Character a=127; Character c=127; System.out.println(a==c);//输出true

            boolean所对应包装类的数据缓存为true和false;

    Boolean a =true; Boolean c =true; System.out.println(a==c);//输出true 自动拆箱和装箱的过程由编译器自动完成:通过包装类的 valueOf 方法将基本数据类型包装成引用类型; 通过包装类对象 xxxValue 方法将引用类型变为对应的基本类型,所以上页代码经过编译器编译后为: Integer a =12; int aa =a ; System.out.println(aa);//12  

    自动拆箱:和基本数据类型比较时自动变成基本数据类型

    int c =128; Integer cc=128; Integer ccc= new Integer(128); System.out.println(c==cc);//true System.out.println(c==ccc);//true System.out.println(cc==ccc);//false

     

    Processed: 0.010, SQL: 9