JavaSE——三个特殊的类(5)

包装类

1.包装类基本原理

包装类就是将基本数据类型封装到类中。

//这里的IntDemo实际上就是int数据类型的包装类
class IntDemo{
    private int num;
    public IntDemo(int num){
        this.num = num;
    }
    public int intValue(){
        return this.num;
    }
}

public class test{
    public static void main(String[] args) {
        //子类对象向上转型
        Object obj = new IntDemo(55);
        //向下转型
        IntDemo temp = (IntDemo)obj;
        //取出里面的基本数据类型操作
        System.out.println(temp.intValue());
    }
}
//输出
//55

综上,将基本数据类型包装为一个类对象的本质就是使用Object进行接收处理

Java中有8种数据类型,如果每种都按照以上方式编写存在以下问题:

  • 开发中代码重复
  • 在进行数学计算的时候,必须利用明确的方法将包装的数据取出后才能进行运算

为了便于开发,专门提供了包装类的使用,而关于包装类的使用又提供了两种类型:

  • 对象型(Object的直接子类):Boolean、Character(char)
  • 数值型(Number的直接子类):Byte、Double、Short、Long、Integer(int)、Float

说明:关于Number类

  • Number类的定义:public abstract Number implements java.io.Serializable
  • 在Number类里面实际定义有六种重要方法

2.装箱与拆箱

装箱:将基本数据类型变为包装类对象,利用每个包装类提供的构造方法实现装箱处理
拆箱:将包装类中包装的基本数据类型取出(利用Number类中提供的六种方法)。

public class test{
   public static void main(String[] args) {
       Integer num = new Integer(55);//装箱
       int data = num.intValue();//拆箱
       System.out.println(data);
   }
}
//输出
//55

如上操作是手工的装箱和拆箱。在JDK1.5之后提供了自动拆装箱机制,由于此机制的存在,可以直接利用包装类进行各种数学计算,如下

public class test{
   public static void main(String[] args) {
       //自动装箱
       Integer x = 55;
       //可以直接利用包装类对象操作
       System.out.println(++x);
   }
}
//输出
//56

注意:这里存在一个“==”和“equals”问题,在阿里编码规范中,所有相同类型的包装类对象之间的值比较全部使用equals方法

public class test{
    public static void main(String[] args) {
        Integer num1 = new Integer(10);
        Integer num2 = new Integer(10);
        System.out.println(num1 == num2);//false
        System.out.println(num1 == new Integer(10));//false
        System.out.println(num1.equals(num2));//true
        System.out.println(num1.equals(new Integer(10)));//true
    }
}

!!!!说明:
       对于Integer var = ?在-128至127范围内的赋值,Integer对象是在IntegerCache.cache产生,会复用已有对象,这个区间内的Integer值可以直接使用“==”判断,但是这个区间外的所有数据都会在堆上产生,并不会复用已有对象,所有推荐使用equals进行比较。

3.使用基本数据类型还是包装类

根据阿里编码规范

  • 【强制】所有的POJO类属性必须使用包装数据类型
  • 【强制】RPC方法的返回值和参数必须使用包装数据类型
  • 【推荐】所有的局部变量使用基本数据类型

4.字符串与基本数据类型转换

       以后进行各种数据的输入,一定都是字符串类型接收。所以在开发中,如何将字符串变为各个数据类型就需要包装类的支持。

  • String变int类型(Integer类)
public static int parsrInt(String s)throws NumberFormatException

String str = "123";//String类型
int num = Integer.parseInt(str);
System.out.println(num);//123
  • String变double类型(Double类)
public static int parseDouble(String s)throws NumberFormatException

String str = "123" ; // String类型
double num = Double.parseDouble(str) ;
System.out.println(num);//123.0

       需要注意的是, 将字符串转为数字的时候, 字符串的组成有非数字, 那么转换就会出现错误(NumberFormatException

String str = "123aassa" ; // String类型
double num = Double.parseDouble(str) ;
System.out.println(num);
//Exception in thread "main" java.lang.NumberFormatException: For input string: "123aassa"
  • String变boolean类型(Boolean类)
public static boolean parseBoolean(String s)

String str = "346" ; // String类型
boolean num = Boolean.parseBoolean(str) ;
System.out.println(num);//false

如果现在要把基本数据类型变为字符串:

  • 任何数据类型使用了“+”连接空白字符串就变为了字符串类型
  • 使用String类中提供的valueOf()方法,此方法不产生垃圾
String str = String.valueOf(100) ;
System.out.println(str);//100

猜你喜欢

转载自blog.csdn.net/LiLiLiLaLa/article/details/86500571