zoukankan      html  css  js  c++  java
  • 基本数据类型的包装类

    List <Integer> list = new ArrayList();

    01.集合中的泛型类型<int> ,不允许出现基本数据类型

    02.定义了一个基本数据类型的变量,变量名能点出来东西  int num = 5;  num.????   包装类可以

    03.基本和数据类型不能装换成对象,包装类可以

    04.所有的包装类都是由final修饰的,不允许被继承

    05.在基本数据类型需要转换成对象的时候,使用包装类

    06.在jdk1.5之后,允许基本数据类型和包装类进行混合运算,底层有装箱和拆箱操作

    基本数据了类型               包装类

          byte                                Byte

          short                               Short

           int                                   Integer

           long                                Long

           float                                 Float

           double                            Double

    上面的六都是数值类型!都是 extends Number inplements Comparable<T>  ,Numbe类实现了Serializable 接口 ,支持序列化和反序列化

            char                               Character

            boolean                         Boolean

    上面两种都是  implements java.io.Serializable ,Comparable<T>

    比如说:

        public  Serializable getNum(Serializable s){

        }

        我们调用getNum(参数可以是8种包装类中的任何一个),返回值也可以是8种包装类的任何一个。

    装箱和拆箱    =====》包装类和对应基本数据类型的转换

         装箱:  把基本数据类型转换成对应的包装类      Integer num = 1;

         拆箱:  把包装类转换成对应的基本数据类型      int  num2 = num;

        /**
         * 01.所有的包装类 都有将对应的基本数据类型作为参数 来构造自己的实例
         */
        @Test
        public void test01() {
            Byte b = new Byte((byte) 1);
            Short s = new Short((short) 1);
            Integer i = new Integer(1);
            Long l = new Long(1);
            Float f = new Float(1.0);
            Double d = new Double(5.2);
    
            Character c = new Character('1'); // char的数值取值范围  0-65535
            Character c2 = new Character((char) 50);
            Boolean bo = new Boolean(true);
            /**
             * 01.Float有三种实例化的方法  参数分别是double   float  和 String
             * 02.除了Character以外的7种包装类 都有将  String作为参数 来构建自己的实例
             */
        }
    
        /**
         * 02.除了Character以外的7种包装类 都有将  String作为参数 来构建自己的实例
         * 
         * 6中数值类型的包装类都继承了Number
         * 所以在用String作为参数来创建自己实例的时候,
         * 如果参数不能转换成数值 则抛出 NumberFormatException
         */
        @Test
        public void test02() {
            Byte b = new Byte("1");
            Short s = new Short("1");
            Integer i = new Integer("1");
            Long l = new Long("1");
            Float f = new Float("1");
            Double d = new Double("1");
    
            // Character c = new Character("1"); 编译报错
            Boolean bo = new Boolean("TRUE"); // 除了 大小写的true 返回 true 其余全部是false
            System.out.println(bo);
        }
    
        /**
         * 03.除了Character以外的7种包装类 都有parseXxx(String s)
         * 
         *   比如说Byte b = new Byte("1");
         *        b.parseByte(String)
         *      001. 四种整型对应的包装类 都是 parseXxx(String s,int radix)radix 进制转换
         *      002. 其他的四种都没有 parseXxx(String s,int radix)
         *      003. Character压根没有parseXxx()
         */
        @Test
        public void test03() {
            Byte b = new Byte("1");
            Short s = new Short("1");
            Integer i = new Integer("1");
            Long l = new Long("1");
    
            Float f = new Float("1");
            Double d = new Double("1");
            Character c = new Character('1'); // 没有parseXxx
            Boolean bo = new Boolean("TRUE");
        }
    
        /**
         * 04.进制转换  需要学习 位运算
         */
        @Test
        public void test04() {
            System.out.println("2进制的10对应的数据===》" + Integer.toBinaryString(10));
            System.out.println("8进制的10对应的数据===》" + Integer.toOctalString(10));
            System.out.println("16进制的10对应的数据===》" + Integer.toHexString(10));
        }
    
        /**
         * 05.valueOf
         *      把基本数据类型转换成对应的包装类  ===》装箱
         *      除了Character没有参数是String类型的方法
         *   
         *    xxxValue      8中保证类型都有
         *     把xxx类型转换成对应的基本数据类型  ===》拆箱
         */
        @Test
        public void test05() {
            int num = 5;
            Integer i = Integer.valueOf(num); // 装箱
            num = i.intValue(); // 拆箱
        }
    
        /**
         * 06.经典的面试题
         *  因为Integer.valueOf() 会缓存  -128 到  127 之间的数据!
         *  如果我们的数字在这个区间,不会去创建新的对象,而是从缓存池中获取!
         *  否则就new  Integer();
         */
        @Test
        public void test06() {
            int num1 = 127;
            int num2 = 127;
            System.out.println(num1 == num2); // true
            Integer a1 = new Integer(127);
            Integer b1 = new Integer(127);
            System.out.println(a1 == b1);// false
            Integer a = 127;
            Integer b = 127;
            System.out.println(a == b);// true
            Integer c = 128;
            Integer d = 128;
            System.out.println(c == d); // false
        }
    
        @Test
        public void test07() {
            System.out.println("1" + 1 + 1);// 111
            System.out.println(1 + 1 + "1" + 1);// 211
        }
    }

       

  • 相关阅读:
    VS2005 DataGridView 和 GirdView 横向大比拼
    表结构信息查询
    在自己的网页中嵌入搜索引擎
    自定义AJAX请求获取地图范围
    oracle远程连接配置
    oracle账户被锁定问题
    JDK环境配置
    PythonWin运行出错解决办法
    HDF库的调试过程
    ajax入门详解
  • 原文地址:https://www.cnblogs.com/liutianci/p/8110326.html
Copyright © 2011-2022 走看看