zoukankan      html  css  js  c++  java
  • Java API ——包装类

    1、包装类的概述

      · 将基本数据类型封装成对象的好处在于可以在对象中定义更多的功能方法操作该数据。            

      · 常用的操作之一:用于基本数据类型与字符串之间的转换。            

      · 基本类型和包装类的对应        

      为了对基本数据类型进行更多的操作,更方便的操作,Java就针对每一种基本数据类型提供了对应的类类型。包装类类型。
      byte      Byte
      short    Short 
      int     Integer  
      long    Long 
      float    Float
      double   Double
      char    Character  
      boolean   Boolean
     
    2、Integer类
     
    /**
     * Integer的构造方法:
     * public Integer(int value)
     * public Integer(String s)
     *         注意:这个字符串必须是由数字字符组成
     */
    public class IntegerDemo01{
        public static void main(String[] args) {
            //方式一:
            int i =100;
            Integer ii = new Integer(i);
            System.out.println("ii:"+ii); //ii:100
            //方式二:
            String s = "100";
            Integer iii = new Integer(s);
            System.out.println("iii:"+iii); //iii:100
        }
    }

     
    成员方法:
     
    public class IntegerDemo02 {
        public static void main(String[] args) {
            //输出100的二进制,八进制和十六进制
            System.out.println(Integer.toBinaryString(100)); //1100100
            System.out.println(Integer.toOctalString(100)); //144
            System.out.println(Integer.toHexString(100)); //64
            System.out.println(Integer.MAX_VALUE); //2147483647
            System.out.println(Integer.MIN_VALUE); //-2147483648
        }
    }

    3、int类型和String类型的相互转换

      · public int intValue():以int类型返回该Integer的值            
      · public static int parseInt(String s):将字符串参数作为有符号的十进制整数进行解析           
      · public static String toString(int i):返回一个表示指定整数的String对象           
      · public static Integer valueOf(int i):返回一个表示指定的int值的Integer实例           
      · public static Integer valueOf(String s):返回一个表示指定的String值的Integer实例
    //int --> String : String.valueOf(int)
    //String --> int : Integer.paraseInt(String)
    public class IntegerDemo03 {
        public static void main(String[] args) {
            //int --> String
            int num = 100;
            //方式一:
            String s1 = "" + num;
            System.out.println("s1:" + s1);
            //方式二:推荐
            String s2 = String.valueOf(num);
            System.out.println("s2:"+s2);
            //方式三:
            //int --> Integer --> String
            String s3 = new Integer(num).toString();
            System.out.println("s3:"+s3);
            //方式四:
             String s4 = Integer.toString(num);
            System.out.println("s4:"+s4);
            System.out.println("----------------");
            //String --> int
            //方式一:String --> Integer --> int
            //public int intValue();
            String s = "100";
            Integer i =  new Integer(s);
            int i1 = i.intValue();
            System.out.println("i1:"+i1);
            //方式二:Integer.paraseInt(s):推荐
            int i2 = Integer.parseInt(s);
            System.out.println("i2:"+i2);
        }
    }
     
    4、常用的基本进制转换
      A:十进制转成二进制,八进制和十六进制
        · public static String toBinaryString(int i)            
        · public static String toOctalString(int i)            
        · public static String toHexString(int i)       
      B:十进制到其他进制            
        · public static String toString(int i,int radix)           
        范围是从2到36
            C:其他进制到十进制            
        · public static int parseInt(String s,int radix)
    public class IntegerDemo04 {
        public static void main(String[] args) {
            //十进制到二进制,八进制,十六进制
            System.out.println(Integer.toBinaryString(100)); //1100100
            System.out.println(Integer.toOctalString(100));//144
            System.out.println(Integer.toHexString(100)); //64
            //十进制到其他进制
            System.out.println(Integer.toString(100, 10)); //100
            System.out.println(Integer.toString(100,2)); //1100100
            System.out.println(Integer.toString(100,36)); //2s
            System.out.println(Integer.toString(100, 37)); //100
            //其他进制到十进制
            System.out.println(Integer.parseInt("100",10)); //100
            System.out.println(Integer.parseInt("100",2));  //4
            //报错,因为二进制中没有2,3
            //System.out.println(Integer.parseInt("123",2));
        }
    }
     
    5、JDK5的新特性:
        自动装箱:把基本类型转成包装类类型
        自动拆箱:把包装类类型转成基本类型
           Integer x = new Integer(4);可以直接写成    Integer x = 4;//自动装箱。    
      x  = x + 5;//自动拆箱。通过intValue方法。    
     需要注意:    
      在使用时,Integer  x = null;上面的代码就会出现NullPointerException。
    public class IntegerDemo05 {
        public static void main(String[] args) {
            //定义了一个int类型的包装类类型变量i
            //Integer i1 = new Integer(100);
            Integer i2 = 200;
            i2 += 300;
            System.out.println("i2:"+i2);
            //通过反编译后的代码
            //Integer i2 = Integer.valueOf(200); //自动装箱
            //i2 = Integer.valueOf(i2.intValue() + 200);
            //System.out.println((new StringBuilder("i2:")).append(i2).toString());
        }
    }
     
    6、Integer直接赋值的面试题
        注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里直接获取数据
    public class IntegerDemo06 {
        public static void main(String[] args) {
            Integer i1 = new Integer(127);
            Integer i2 = new Integer(127);
            System.out.println(i1 == i2); //false
            System.out.println(i1.equals(i2)); //true
            System.out.println("---------------");
            Integer i3 = new Integer(128);
            Integer i4 = new Integer(128);
            System.out.println(i3 == i4); //false
            System.out.println(i3.equals(i4)); //true
            System.out.println("---------------");
            Integer i5 = 128;
            Integer i6 = 128;
            System.out.println(i5 == i6); //false
            System.out.println(i5.equals(i6)); //true
            System.out.println("---------------");
            Integer i7 = 127;
            Integer i8 = 127;
            System.out.println(i7 == i8); //true
            System.out.println(i7.equals(i8)); //true
            Integer i = Integer.valueOf(127);
            //通过查看源码,针对-128到127之间的数据做了一个数据缓冲池,
            //如果一个数据是在该范围内的,每次都并不创建新的空间,
            //如果不在这个范围里面的就会创建新的对象
            // public static Integer valueOf(int i) {
            // assert IntegerCache.high >= 127;
            // if (i >= IntegerCache.low && i <= IntegerCache.high)
            // return IntegerCache.cache[i + (-IntegerCache.low)];
            // return new Integer(i);
        }
    }
     
     
     
     
     
  • 相关阅读:
    前端生成pdf文件之pdfmake.js
    vim 安装
    linux基础学习
    python 编码处理
    Ubuntu 下配置 SSH服务全过程及问题解决
    yum 安装
    Ubuntu安装MySQL
    Linux各发行版本及其软件包管理方法
    轻松学习LINUX系列教程推出
    常用命令
  • 原文地址:https://www.cnblogs.com/yangyquin/p/4949487.html
Copyright © 2011-2022 走看看