8大基本数据类型及其包装类总结

来源:互联网 发布:淘宝助理怎么打印订单 编辑:程序博客网 时间:2024/05/02 22:50
 //boolean(JVM规范中,boolean变量作为int处理,也就是4字节;boolean数组当做byte数组处理。) byte1 short2 char2 int4 float4 double8 long8
//        在Java编译器中,把原始类型(8种)自动转换为封装类的过程,称为自动装箱,相当于valueOf方法。
//      对于当下32位的处理器(CPU)来说,一次处理数据是32位(这里不是指的是32/64位系统,而是指CPU硬件层面),32 位 CPU 使用 4 个字节是最为节省的,哪怕你是 1 个 bit 他也是占用 4 个字节。因为 CPU 寻址系统只能 32 位 32 位地寻址,具有高效存取的特点。
//        例如:
//
//        Integer i=10;这行代码是等价于Integer i=Integer.valueOf(10);的。根据jdk源码,valueOf方法是先去一个叫IntegerCache类(是一个Integer数组cache[]的再封装)里面查找这个数是否被缓存了,如果有,直接拿出来,如果没有则new一个对象。
//
//        IntegerCache的缓存范围是从-128到high,这个high是通过JVM的启动参数指定的。
//        int a=1;
//        Integer b=1;//1是緩衝區裡面的
//        Integer c=new Integer(1);//new 出來的
//        Integer d=Integer.valueOf(1);//根据jdk源码,valueOf方法是先去一个叫IntegerCache类
//        //(是一个Integer数组cache[]的再封装)里面查找这个数是否被缓存了,如果有,直接拿出来,如果没有则new一个对象。1是緩衝區裡面的
//        System.out.println(a==b);//true,因为涉及到基本类型的比较,要解包
//        System.out.println(a==c);//true
//        System.out.println(b==c);//b是缓存区里拿出来的,c是new出来的,所以false
//        System.out.println(c==d);//d也是缓存区里拿出来的,所以b==d是true,c==d是false
//        System.out.println(b==d);//true
        
//        src      the source array.
//        * @param      srcPos   starting position in the source array.
//        * @param      dest     the destination array.
//        * @param      destPos  starting position in the destination data.
//        * @param      length   the number of array elements to be copied.
//        System.arraycopy(src, srcPos, dest, destPos, length);
        
        
//        Integer i=129;
//        System.out.println(129==i);//比的是值    自动拆装箱true
//        Integer k=129;
//        Integer j=129;
//        System.out.println(k==j);//不在缓冲区,是new出来的两个对象false
//        System.out.println(k.equals(j));//比的是值true
        
//        Object obj=new Object();
//        Object obj1=new Object();
//        System.out.println(obj==obj1);//false
//        System.out.println(obj.equals(obj1));//false
        
//        String str="123";//没有new
//        String str1=new String("123");//new
//        String str2=String.valueOf(123);//new
//        String str3=String.valueOf("123");//没有new
//        
//        System.out.println(str==str1);//false
//        System.out.println(str.equals(str1));//true    重写equals比的是值
//        
//        System.out.println(str==str2);//false
//        System.out.println(str.equals(str2));//true
//        
//        System.out.println(str==str3);//true
//        System.out.println(str.equals(str3));//true
//        
//        System.out.println(str1==str3);//false
        //总结:对于String eaquals总是true的 ,而==只有在   (没有new==没有new)->true
        
        //Double和Float没有缓冲区
//        Double a=12D;
//        Double b=12D;
//        System.out.println(a==b);//false  没有缓冲区  是new出来的  所以不可能相等
//        System.out.println(a.equals(b));//true 重写比值
        
//        Float a=12f;
//        Float b=12f;
//        System.out.println(a==b);//false  没有缓冲区  是new出来的  所以不可能相等
//        System.out.println(a.equals(b));//true 重写比值
        
//        double c=1;
//        double d=1;

//        System.out.println(c==d);//true  对于8大基本数据类型是可以直接比值的

          short s=1;

          s=s+1;编译报错

          s+=1;编译不报错相当于s=(short)(s+1)隐含的强制转换 

        

        //对于其他的包装类  请参照Integer缓冲区    包装类除了Double和Float 都有包装类Cache(例如:IntergerCache)

要想弄明白,看源码,看源码,看源码,光看我博客也没用。