JavaSE--基本数据类型的包装类

发布时间 2023-08-15 21:43:27作者: 洛小依ovo

一、八种包装类

1、为什么需要基本数据类型的包装类

  有以下需求:方法参数为Object obj,但是有需要传进去一个数字或者字符,

public class IntegerTest01 {
    public static void main(String[] args) {
        // 有没有这种需求:调用doSome()方法的时候需要传一个数字进去
        // 但是数字属于基本数据类型,而doSome()方法参数的类型是Object
        // 可见doSome()方法无法接收基本数据类型的数字可以传一个数字对应的包装类进去

        // 把100这个数字经过构造方法包装成对象
        MyInt myInt = new MyInt(100);
        // doSome()方法虽然不能直接传100,但是可以传一个100对应的包装类型。
        doSome(myInt);
    }

    public static void doSome(Object obj){
        System.out.println(obj);
    }
}

2、八种包装类型

byte                    java.lang.Byte
short                   java.lang.Short
int                     java.lang.Integer
long                    java.lang.Long
float                   java.lang.Float
double                  java.lang.Double
boolean                 java.lang.Boolean
char                    java.lang.Character

 

二、Integer(其他的包装类和Integer用法一样)

1、Integer类的构造方法

// Integer(int)
// Integer(String)
public class IntegerTest03 {
    public static void main(String[] args) {

        // 将数字100转换成Integer包装类型(int --> Integer)
        Integer x = new Integer(100);
        System.out.println(x);

        // 将String类型的数字,转换成Integer包装类型(String --> Integer)
        Integer y = new Integer("123");
        System.out.println(y);

        // double -->Double
        Double d = new Double(1.23);
        System.out.println(d);

        // String --> Double
        Double e = new Double("3.14");
        System.out.println(e);
        
        Integer a = new Integer("中文");// 报错 数字格式化异常
    }
}

2、访问包装类的常量获取最大值最小值

System.out.println("int的最大值:" + Integer.MAX_VALUE);
System.out.println("int的最小值:" + Integer.MIN_VALUE);
System.out.println("byte的最大值:" + Byte.MAX_VALUE);
System.out.println("byte的最小值:" + Byte.MIN_VALUE);

3、Integer类常用的方法

// 手动装箱
Integer x = new Integer(1000);

// 手动拆箱
int y = x.intValue();
System.out.println(y);
// static int parseInt(String s)
// 静态方法,传参String,返回int
int retValue = Integer.parseInt("123"); // String -转换-> int
//int retValue = Integer.parseInt("中文"); // NumberFormatException
System.out.println(retValue + 100);

// 其他包装类也有
double value2 = Double.parseDouble("3.14");
System.out.println(value2 + 1);
// static String toBinaryString(int i)
// 静态的:将十进制转换成二进制字符串
String binaryString = Integer.toBinaryString(3);
System.out.println(binaryString);

// static String toHexString(int i)
// 静态的:将十进制转换成十六进制字符串
String hexString = Integer.toHexString(16);
System.out.println(hexString);

//static String toOctalString(int i)
// 静态的:将十进制转换成八进制字符串
String octalString = Integer.toOctalString(8);
System.out.println(octalString); // "10"

//static Integer valueOf(int i)
// 静态的:int-->Integer
Integer i1 = Integer.valueOf(100);
System.out.println(i1);

// static Integer valueOf(String s)
// 静态的:String-->Integer
Integer i2 = Integer.valueOf("100");
System.out.println(i2);

 

三、装箱、拆箱

1、装箱、拆箱

  装箱:基本数据类型--转换为-->引用数据类型

  拆箱:引用数据类型--转换为-->基本数据类型

public class IntegerTest02 {
    public static void main(String[] args) {

        // 123这个基本数据类型,进行构造方法的包装达到了:基本数据类型向引用数据类型的转换
        // 基本数据类型 -(转换为)->引用数据类型(装箱)
        // 手动装箱
        Integer i = new Integer(123);

        // 将引用数据类型--(转换为)-> 基本数据类型
        // 手动拆箱
        float f = i.floatValue();
        System.out.println(f); //123.0

        // 将引用数据类型--(转换为)-> 基本数据类型(拆箱)
        // 手动拆箱
        int retValue = i.intValue();
        System.out.println(retValue); //123
    }
}
// Number是一个抽象类,无法实例化对象。
Number类中有这样的方法:
    byte byteValue() 以 byte 形式返回指定的数值
    abstract  double doubleValue()以 double 形式返回指定的数值
    abstract  float floatValue()以 float 形式返回指定的数值
    abstract  int intValue()以 int 形式返回指定的数值
    abstract  long longValue()以 long 形式返回指定的数值
    short shortValue()以 short 形式返回指定的数值
// 这些方法其实所有的数字包装类的子类都有,这些方法是负责拆箱的

2、自动装箱、自动拆箱

jdk1.5之后有了自动装箱和自动拆箱

  自动装箱:基本数据类型自动转换成包装类

  自动拆箱:包装类自动转换成基本数据类型

// 900是基本数据类型,i是包装类型
// 基本数据类型转换为包装类型:自动装箱
Integer i = 900;

// x是基本数据类型,i是包装类型
// 包装类型转换为基本数据类型:自动拆箱
int x = i;

// y还是引用,还是保存的内存地址
Integer y = 100; // 等同于new Integer(100);
// 这里y会自动拆箱,自动转换成基本数据类型
System.out.println(y + 1);

 3、注意

Integer a = 128;
Integer b = 128;
System.out.println(a == b);// false

Integer x = 127;
Integer y = 127;
System.out.println(x == y);// true

// 注意:
    java中为了提高程序的执行效率,将[-128到127]之间所有的包装对象提前创建好,
放到了一个方法区的“整数型常量池”当中了,目的是只要用这个区间的数据不需要
再new了,直接从整数型常量池当中取出来