基本类型包装类

基本类型包装类

参考黑马程序员

基本类型包装类概述

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

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

基本数据类型包装类
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean


Integer类的概述和使用

Integer:包装一个对象中的原始类型int的值

方法名说明
public Integer(int value) 根据int值创建Integer对象(过时)
public Integer(String s) 根据String值创建Integer对象(过时)
public static Integer valueOf(int i) 返回表示指定的int值的Integer实例
public static Integer valueOf(String s) 返回一个保存指定值的Integer对象String

代码

public class IntegerDemo {
   public static void main(String[] args) {
       //public Integer(int value)根据int值创建Integer对象(过时)
       Integer i1 = new Integer(100);
       System.out.println(i1);

       //public Integer(String s)根据String值创建Integer对象(过时)
       Integer i2 = new Integer("100");
//       Integer i2 = new Integer("abc");//NumberFormatException
       System.out.println(i2);
       System.out.println("-----------------------");

       //public static Integer valueOf(int i)返回表示指定的int值的Integer实例
       Integer i3 = Integer.valueOf(100);
       System.out.println(i3);

       //public static Integer valueOf(String s)返回一个保存指定值的Integer对象String
       Integer i4 = Integer.valueOf("100");
//       Integer i4 = Integer.valueOf("abc");//NumberFormatException
       System.out.println(i4);
  }
}


int和String的相互转换

基本类型包装类的最常见操作就是:用于基本类型和字符串之间的相互转换

  1. int转换为String

    public static String valueOf(int i):返回int参数的字符串表示形式。该方法是String类中的方法

  2. String转换为int

    public static int parseInt(String s):将字符串解析为int类型。该方法是Integer类中的方法

代码

/*
   int和String的相互转换
*/
public class IntegerDemo2 {
   public static void main(String[] args) {
       //int --- String
       int number = 100;
       //方式1
       String s1 = "" + number;
       System.out.println(s1);
       //方式2
       //public static String valueOF(int i)
       String s2 = String.valueOf(number);
       System.out.println(s2);
       System.out.println("--------");

       //String --- int
       String s = "100";
       //方式1
       //String --- Integer --- int
       Integer i = Integer.valueOf(s);
       //public int intValue()
       int x = i.intValue();
       System.out.println(x);
       //方式2
       //public static int parseInt(String s)
       int y = Integer.parseInt(s);
       System.out.println(y);

  }
}


自动装箱和拆箱

  • 装箱:把基本数据类型转换为对应的包装类类型

  • 拆箱:把包装类类型装换为对应的基本数据类型

Integer i = 100; //自动装箱

i +=200; //i = i + 200; i + 200 自动拆箱;i = i + 200; 是自动装箱

注意:在使用包装类类型的时候,如果做操作,最好先判断是否为null

我们推荐是,只要是对象,在使用前就必须进行不为null的判断

代码

public class IntegerDemo3 {
   public static void main(String[] args) {
       //装箱:把基本数据类型转换为对应的包装类类型
       Integer i = Integer.valueOf(100);
       Integer ii = 100;//Integer.valueOf(100); 自动装箱

       //拆箱:把包装类类型装换为对应的基本数据类型
       // ii += 200;
//       ii = ii.intValue() + 200;//手动拆箱,自动装箱
       ii += 200;//自动拆箱,然后自动装箱
       System.out.println(ii);

       Integer iii = null;//NullPointerException
       if (iii!=null) {
           iii += 200;
      }
  }
}

 

posted @ 2021-03-18 10:54  i爱在西元前  阅读(80)  评论(0)    收藏  举报