zoukankan      html  css  js  c++  java
  • 包装类相关例子

    public class Test {
    /**
     * 01.所有的包装类 都有将对应的基本数据类型作为参数  来构造自己的实例
     */
     public void test1(){
      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作为参数 来构建自己的实例
       *
       */
     }
     
     
     
     /**
      * 六种数值类型的包装类都继承课Number
       * 所以在用String作为参数来创建自己实例的时候,
       * 如果参数不能转换成数值 则抛出NumborFormatExcption
      */
     public static void test2(){
      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("1");//除了大小写的true 返回true,其余全返回false
      Boolean bo1=new Boolean("true");
      System.out.println(bo);
      System.out.println(bo1);
      
     }
     public static void main(String[] args) {
      test2();
     }
     
     
     /**
      * 除了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
      */
     
     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 bo1=new Boolean("true");
      
     }
     
     /**
      * 04.进制转换 需要学习 位运算
      */
     public void test04(){
      System.out.println("2进制的10对应的数据"+Integer.toBinaryString(10));
      System.out.println("8进制的10对应的数据"+Integer.toHexString(10));
      System.out.println("16进制的10对应的数据"+Integer.toOctalString(10));
     }
     
     /**
      * 05.valueOf
      *   把基本数据类型转换成对应的包装类===》装箱
      *   除了Character没有参数String类型的方法
      *
      * xxxValue     8种保证类型都有
      * 把xxx类型转换成对应的基本数据类型===》拆箱
      *
      */
     
     public void test05(){
      int num=5;
      Integer i=Integer.valueOf(num);//装箱
      num=i.intValue();//拆箱
     }
     
     /**
      * 06.经典的面试题
      * 因为Integer.valueOf()会缓存-128到127之间的数据
      * 如果我们的数字在这个区间,不会去创建新的对象,而是从缓存池中获取!
      * 否则级new Integer();
      */
     public void test06(){
      int num1=127;
      int num2=127;
      System.out.println(num1==num2);//true
      Integer a1=127;
      Integer a2=127;
      System.out.println(a1==a2);//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
     }
     
     public void test07(){
      System.out.println("1"+1+1);//111
      System.out.println(1+1+"1"+1);//211
     }
     
     

  • 相关阅读:
    图解Http协议 url长度限制
    cpu占用工具,亲测可用
    python使用mysql 获取数据,感知不到数据变化的原因
    dns压测工具dnsperf
    sqlalchemy.exc.ArgumentError: Mapper mapped class Result->result could not assemble any primary key 报错解决
    DevTools failed to load SourceMap: Could not load content for chrome-extension 解决
    tcp/udp 的一些测试
    win端 vscode 远程连接 centos,配置调试
    gitllab访问报错:Permission denied (publickey). 以及后续测试
    linux文件表项测试
  • 原文地址:https://www.cnblogs.com/WillimTUrner/p/8108828.html
Copyright © 2011-2022 走看看