一、包装类的创建
1.包装类定义
(1)定义:每一种基本数据类型对应的类,以对象形式表现的基本数据类型(包装类型)
(2)有8大基本数据类型,其对应的包装类分别为:
类型 | 字节型 | 字符型 | 布尔型 | 短整型 | 整型 | 长整型 | 单精度实型 | 双精度实型 |
---|---|---|---|---|---|---|---|---|
基本数据类型名 | byte | char | boolean | short | int | long | float | double |
包装类名 | Byte | Character | Boolean | Short | Integer | Long | Float | Double |
2.包装类对象的创建方式
(1)构造方法:new关键字
Integer integer = new Integer(10);//构造的同时赋值
Boolean b = new Boolean(false);
(2)valueOf():也可产生包装类对象
Boolean b = Boolean.valueOf(true);
Character c1= Character.valueOf('a');
Integer i = Integer.valueOf(10);
(3)自动打包:基本数据类型☞包装类数据数据类型
Integer i = 9;
Boolean b = false;
(4)接着来了解一下打包和解包机制:
- 打包:基本数据类型☞包装类数据数据类型
- 打包分为:自动打包和手动打包(包装类对象的3种构建方式即为打包方式)
- 解包:基本数据类型☞包装类数据数据类型
- 解包分为:自动解包和手动解包
- 举个 例子
Integer i1= new Integer(10); // 手动打包
Boolean b = Boolean.valueOf(true);//自动打包
Integer i3= 10; // 自动打包
int i2= i1.intValue(); // 手动解包
int i4= i3; // 自动解包
二、包装类的使用
1.基本数值类型的包装类
基本数值类型有6类:除去boolean类型和字符类型
(1)包装类的数值☞基本数据类型:手动解包
byte byteValue(); //返回byte型数值
short shortValue(); //返回short型数值
int intValue(); //返回int型数值
long longValue(); //返回long型数值
float floatValue(); //返回float型数值
double doubleValue(); //返回double型数值
举个例子:
Integer integer = new Integer(10);
int i= integer.intValue();
(2)包装对象☞字符串:toString方法(既是类方法又是实例方法)
Integer i = new Integer(10);
String str1 = Integer.toString(i);
String str2 = i.toString();
Float f = new Float(123.45);
String str3 = Float.toString(f);
String str4= f.toString();
(3)字符串☞简单数据类型:每个包装类定义的一个类方法,显式的将数字组成的字符串转化为基本数据类型的数据
byte b = Byte.parseByte("10");
short s = Short.parseShort("12");
int i = Integer.parseInt("12");
long l = Long.parseLong("12");
float f = Float.parseFloat("123");
double d = Double.parseDouble("123.4223");
(4)十进制整数☞其它数制表示的字符串。Interger类中定义了将十进制数转化为其它数制表示的字符串的类方法
int i = Integer.parseInt("12");
Integer.toBinaryString(i);//返回2进制无符号整数字符串,1100
Integer.toOctalString(i);//返回8进制无符号整数字符串,14
Integer.toHexString(i);//返回16进制无符号整数字符串,c
2.布尔类型的包装类
Boolean
类与数值类型的包装类一样,提供许多方法,同上述内容相似,也有3种常用方法:
booleanValue();//将Boolean对象的值以对于的boolean值返回
toString();//
parseBoolean();//将字符串转化为boolean值
3.字符类型的包装类
Character
类在对象中包装了一个基本类型为char值,常用方法为:
charValue();//返回char型数值,实例方法
isUpperCase(char ch);//判断参数字符是否为大写字母,类方法
isLowerCase(char ch);//判断参数字符是否为小写字母,类方法
toUpperCase(char ch);//将参数字符转化为大写字母,类方法
isDigit(char ch);//判断是否为数字,类方法
isLetter(char ch);//判断是否为字母,类方法
4.举个例子测试包装类使用
package com.chapter4;
/**
* @Author:
* @Time: 2021/7/24 13:52
* @Description
*/
public class Example4_14 {
public static void main(String[] args) {
int i1 = 1;//简单数据类型
System.out.println("i" + i1);
Integer i2 = new Integer(2);
System.out.println("i2" + i2);
Integer i3 = Integer.valueOf(3);
System.out.println("i3"+i3);
Integer i4 = 4;//自动打包
System.out.println("i4*2="+i4*2);//自动解包
Float f =i2.floatValue();//返回float型数值
// Float b = i1.floatValue();//错误,i1不是对象
System.out.println("f"+f);
String str = f.toString();//转化为字符串
System.out.println("str"+str);//str=2
System.out.println(Float.toString(i1));//输出1.0
System.out.println(Integer.parseInt("12"));//输出12
System.out.println(Long.parseLong("12"));//输出12
System.out.println(Byte.parseByte("12"));//运行错误,越界
int i5 = Integer.parseInt("235");
System.out.println("i5="+i5);//输出i5=235
boolean b1 =Boolean.parseBoolean("true");
System.out.println("b1="+b1);//b1=true
boolean b2 = false;
System.out.println("b2="+b2);//b2=false
Character c= 97;
System.out.println(c.charValue());//97
System.out.println(Character.isUpperCase('A'));//true
System.out.println(Character.toLowerCase('A'));//false
System.out.println(Character.isDigit('A'));//false
System.out.println(Character.isDigit('1'));//true
System.out.println(Character.isLetter('A'));//true
System.out.println(Character.toUpperCase('a'));//A
System.out.println(Character.toLowerCase('A'));//a
}
}
三、基本数据类型和包装类型区别
掌握了包装类的创建以及简单使用,接着了解一下包装类以及基本数据类型的区别。
1. 两者区别
- 包装类型可以为null,基本类型不可以
- 包装类型可用于泛型,而基本类型不可以:泛型在编译时会进行类型擦 除,最后只保留原始类型,而原始类型只能是 Object 类及其子类
- 基本类型比包装类型更高效:基本类型在栈中直接存储的具体数值,而包装类型则存储的是堆中的引用
- 两个包装类型的值可以相同,但却不相等
2.举例
Integer i1= new Integer(10);//手动打包
Integer i2= new Integer(10);//手动打包
System.out.println(i1 == i2); // false
System.out.println(i1.equals(i2)); // true
// 1)基本类型和包装类型
int a = 100;
Integer b = 100;//自动打包
System.out.println(a == b); //true
// 2)两个包装类型
Integer c = 100;
Integer d = 100;//自动打包
System.out.println(c == d); //true
// 3)
c = 200;
d = 200;//自动打包
System.out.println(c == d); //false
分析:
- 比较详情可参考字符串基本描述,主要是存储的栈堆地址的区别
- 了解上述的打包解包机制后,我将打包解包打在了代码的后面:
i1和i2两是包装类每次创建的一个新的对象,堆内存地址都是不一样的,进行 == 比较后输出false;
第一段代码,基本类型和包装类型进行 == 比较,这时候 b 会自动解包为基本数据类型,直接和基本数据类型a 比较值,最后是两个基本数据类型比较,两者都存储在栈内存中指向相同地址的常量池,输出为true;
第二段代码,两个包装类型都被赋值为了 100,这时候会进行自动打包,如果数字在 -128 至 127 之间时,会直接使用缓存中的对象,而不是重新创建一个对象,所以两者指向相同对象;
第三段代码,两个包装类型重新被赋值为了 200,这时候仍然会进行自动打包,赋值的数字不在 -128 至 127 之间,故要new新对象,堆内存地址都是不一样的,故输出为false