包装和数据集

    技术2023-12-23  98

    拆箱和装箱: 装箱:基本数据类型转换为包装类,int->Integer;

    Integer a = 12;

    拆箱:包装类类型转换为基本数据类型,int  ->Integer;

    Integer a = 12; int b = a;

    特点: 缓存:1、如果包装类类型为Byte、Short、Integer、Long, 而且它们的数值范围为-128~127时,则它们的地址会被缓存起来;

    public static void main(String[] args) { Integer a = 127; Integer b = 127; System.out.println(a==b); } 输出为:true; public static void main(String[] args) { Integer a = 128; Integer b = 128; System.out.println(a==b); } 输出为:false;

             2、Float、Double为小数有近似无穷多个,故不可缓存;

             3、Character 缓存的数值范围为 0~127;

             4、Boolean也缓存;

    public static void main(String[] args) { Integer a = new Integer(-128); Integer aa = new Integer(-128); System.out.println(a==aa); } 输出为: false; //因为new了一个对象所以地址不同,其实包装类在数值范围内与String类似;

    2、自动拆箱:当有基本数据类型与包装类类型比较时,包装类类型会自动拆箱成为基本数据类型再进行比较;

    public static void main(String[] args) { int a = 127; Integer aa = 127; Integer aaa = new Integer(127); System.out.println(a==aa); System.out.println(a==aaa); System.out.println(aa==aaa); } 输出为: true; true; false;

     

    Processed: 0.009, SQL: 9