03包装类(Wrapper)的使用

1.认识包装类

java提供了8种基本数据类型对应的包装类,使得基本数据类型的变量具有类的特征

前6个包装类的父类是Number

基本数据类型 包装类

byte      Byte

short     Short

int      Integer

long      Long

float      Float

double    Double

boolean     Boolean

char     Character

 

2.基本数据类型、包装类、String三者之间的相互转换

总结:掌握三个方法:

包装类的parseXxx()使String类型转换为基本数据类型

包装类的XXXValue()使包装类转换为基本数据类型

String重载的valueOf(Xxx xxx) 使基本数据类型转换为String类型

//基本数据类型---->包装类:调用包装类的构造器
   @Test
   public void test1(){
       int num1 = 10;
       Integer int1 = new Integer(num1);
       System.out.println(int1.toString());

       Integer int2 = new Integer("123");
       System.out.println(int2.toString());

       Float f1 = new Float(12.3f);
       Float f2 = new Float("12.3");
       System.out.println(f1);
       System.out.println(f2);

       Boolean b1 = new Boolean(true);
       Boolean b2 = new Boolean("true123");
       Boolean b3 = new Boolean("TrUe");
       System.out.println(b1);
       System.out.println(b2);
       System.out.println(b3);
  }

   //包装类---->基本数据类型:调用包装类的XXXValue()
   @Test
   public void test2(){
       Integer in1 = new Integer(12);

       int i1 = in1.intValue();
       System.out.println(i1+2);
  }

   /*
   JDK5.0 新特性:自动装箱和自动拆箱
    */
   @Test
   public void test3(){

       //自动装箱:基本数据类型--->包装类
       int num1 = 10;
       Integer in1 = num1;

       //自动拆箱:包装类--->基本数据类型

       int num2 = in1;
       System.out.println(num2);
  }

   //基本数据类型--->String类型:调用String重载的valueOf(Xxx xxx)
   @Test
   public void test4(){
       int num1 = 10;
       //方式1:连接运算
       String s1 = num1 + "";
       //方式2:调用String的valueOf(Xxx xxx)
       float f1 = 12.4f;
       String s2 = String.valueOf(f1);

       Double d1 = new Double(12.5);
       String s3 = String.valueOf(d1);

       System.out.println(s2);
       System.out.println(s3);

  }

   //String类型--->基本数据类型、包装类:调用包装类的parseXxx()
   @Test
   public void test5(){
       //
       String s1 = "123";
       int num1 = Integer.parseInt(s1);
       System.out.println(num1 +1);
  }

3.包装类面试题

面试题一:

以下结果输出都是什么

Object o1 = true ? new Integer(1) : new Double(2.0);
      System.out.println(o1);


Object o2;
      if (true)
          o2 = new Integer(1);
      else
          o2 = new Double(2.0);
      System.out.println(o2);

结果:1.0 1

第一个考虑到三目运算符要求两个类型要统一,所有会是1.0

面试题二:
Integer i = new Integer(1);
      Integer j = new Integer(1);
      System.out.println(i == j);//false

      Integer m = 1;
      Integer n = 1;
      System.out.println(m ==n);//true

      Integer x = 128;//相当于new了一个对象
      Integer y = 128;//相当于new了一个对象
      System.out.println(x ==y);//false

解释:Integer 内部定义了 IntegerCache结构,IntegerCache中定义了Integer[],保存了从-128~127范围的整数。如果我梦使用自动装箱的方式,给Integer赋值的范围在-128~127范围内时,可以直接使用数组中的元素,不用再去new了。目的是提高效率。

 

 

posted @   Endless、  阅读(218)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示