八大包装类

java有八种包装类

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

装箱和拆箱

基本概念

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

手动装箱和手动拆箱

手动装箱

有两种方式

  1. 通过Integer构造方法
  2. 通过Integer的valueOf方法
1
2
3
4
5
6
7
8
9
/**手动装箱*/
int num = 10;
Integer num1 = new Integer(num);
Integer num2 = Integer.valueOf(num);
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// 结果:
// num1 = 10
// num2 = 10

手动拆箱

使用intValue方法

1
2
3
4
5
6
/**手动拆箱*/
Integer num = new Integer(10);
int num1 = num.intValue();
System.out.println("num1 = " + num1);
// 结果:
// num1 = 10

自动装箱和自动拆箱

在jdk5之后,可以自动装箱和自动拆箱,即可以使用=直接赋值

以Integer和int为例,演示自动装箱和自动拆箱

自动装箱

1
2
3
4
5
6
/**自动装箱*/
int num1 = 10;
Integer num2 = num1;
System.out.println("num2 = " + num2);
// 结果:
// num2 = 10

自动拆箱

1
2
3
4
5
6
/**自动拆箱*/
Integer num1 = new Integer(10);
int num2 = num1;
System.out.println("num2 = " + num2);
// 结果:
// num2 = 10

Number接口方法

除Character和Boolean以外的六大包装类都实现了Number接口

Number方法全部如下

作用:可以将包装类转换成指定的基本数据类型

image-20220901105423978

演示

1
2
3
4
5
6
7
8
9
/**演示Number接口方法*/
Integer num = 10;
// 转换
double doubleNum = num.doubleValue();
short shortNum = num.shortValue();
long longNum = num.longValue();
float floatNum = num.floatValue();
byte byteNum = num.byteValue();
int intNum = num.intValue();

Number型包装类通用

这里的这些方法在Integer、Double、Float、Byte、Long、Short这些Number型包装类中通用的,但是这些方法在不同的包装类上又有些差别,注意甄别

以Integer为例演示

toOctalString

有此方法的包装类:Integer、Long,但是细节不一样

将int整数转换成八进制字符串

public static String toOctalString(int i)

1
2
3
4
// 将100转换为8进制数
System.out.println(Integer.toOctalString(100));
// 结果:
// 144

toBinaryString

有此方法的包装类:Integer、Long,但是细节不一样

将int整数转换成二进制字符串

public static String toBinaryString(int i)

1
2
3
4
// 将100转换二进制数
System.out.println(Integer.toBinaryString(100));
// 结果:
// 1100100

toHexString

有此方法的包装类:Integer、Long、Double、Float,但是细节不一样

有此方法的包装类:Integer、Long、

将int整数转换成十六进制字符串

public static String toHexString(int i)

1
2
3
4
// 将100转换十六进制数
System.out.println(Integer.toHexString(100));
// 结果:
// 64

toString

有此方法的包装类:Integer、Long、Double、Float、Short、Byte,但是细节不一样

将int整数转换成字符串

有三种重载形式

  1. public String toString()

    默认形式,原样转换字符串,通过Integer对象调用

  2. public static String toString(int i)

    这个是静态方法,可以通过类名调用

  3. public static String toString(int i, int radix)

​ 这个可以指定转换为几进制数(任意进制)的字符串,通过radix指定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 原样转换
System.out.println(Integer.toString(100));
// 转2进制
System.out.println(Integer.toString(100, 2));
// 转8进制
System.out.println(Integer.toString(100, 8));
// 转16进制
System.out.println(Integer.toString(100, 16));
// 转32进制
System.out.println(Integer.toString(100, 32));
// 输出结果
// 100
// 1100100
// 144
// 64
// 34

compare

有此方法的包装类:Integer、Long、Double、Float、Short、Byte,但是细节不一样

比较两个int数的大小

  1. x < y,结果为-1
  2. x > y,结果为1
  3. x = y,结果为0

public static int compare(int x, int y)

1
2
3
4
5
6
7
System.out.println(Integer.compare(10, 100));
System.out.println(Integer.compare(1000, 100));
System.out.println(Integer.compare(100, 100));
// 输出结果
// -1
// 1
// 0

max

有此方法的包装类:Integer、Long、Double、Float、Short、Byte,但是细节不一样

返回a和b中较大者

public static int max(int a, int b)

1
2
3
4
// 返回19和30中的较大者
System.out.println(Integer.max(19, 30));
// 输出:
// 30

min

有此方法的包装类:Integer、Long、Double、Float、Short、Byte,但是细节不一样

返回a和b较小者

public static int min(int a, int b)

1
2
3
4
// 返回19和30中的较小者
System.out.println(Integer.min(19, 30));
// 输出:
// 19

sum

有此方法的包装类:Integer、Long、Double、Float、Short、Byte,但是细节不一样

计算a和b的和

public static int sum(int a, int b)

1
2
3
4
// 输出20 + 80
System.out.println(Integer.sum(20, 80));
// 输出:
// 10

valueOf

有此方法的包装类:Integer、Long、Double、Float、Short、Byte,但是细节不一样

也是进行类型转换,只不过是转换成Integer

有三种重载形式

  1. public static Integer valueOf(int i)

    将int转换为Integer

  2. public static Integer valueOf(String s)

    将String转换为Integer

  3. public static Integer valueOf(String s, int radix)

    指定String的基数,将String转换为Integer

1
2
3
4
5
6
7
8
9
10
Integer result1 = Integer.valueOf(100);
Integer result2 = Integer.valueOf("100");
Integer result3 = Integer.valueOf("64", 16);
System.out.println(result1);
System.out.println(result2);
System.out.println(result3);
// 结果
// 100
// 100
// 100

praseXXX

praseXXX这个方法代表一类方法

具体有:parseInt|praseLong|praseDouble|praseFloat|praseShort|praseByte

结合具体包装类名称命名

有此方法的包装类:Integer、Long、Double、Float、Short、Byte,但是细节不一样

以parseInt为例

将字符串转换为int整数

有两种重载形式

  1. public static int parseInt(String s)

    将字符串转化为int整数,默认方式

  2. public static int parseInt(String s, int radix)

    指定字符串s的进制,将其转换成int整数

1
2
3
4
5
6
7
// 将"102"转换为整数102
System.out.println(Integer.parseInt("102"));
// 将"64"指定16进制转换为整数(即指定"64"为16进制字符串,要将其转换成十进制100)
System.out.println(Integer.parseInt("64", 16));
// 输出结果
// 102
// 100

isFinite

有此方法的包装类:Double、Float

判断浮点数是否有限

有限返回true,无限(无穷大)返回false

以Double为例

public static boolean isFinite(double d)

1
2
3
4
5
System.out.println(Double.isFinite(7.9));
System.out.println(Double.isFinite(3.0 / 0));
// 结果
// true
// false

isInfinite

有此方法的包装类:Double、Float

判断浮点数是否无限(无穷大)

无限(无穷大返回true),有限返回false

以Double为例

public static boolean isInfinite(double v)

1
2
3
4
5
System.out.println(Double.isInfinite(7.9));
System.out.println(Double.isInfinite(3.0 / 0));
// 结果
// false
// true

isNaN

有此方法的包装类:Double、Float

判断浮点数是否为number

是number返回false,不是number返回true

以Double为例

1
2
3
4
System.out.println(Double.isNaN(Double.NaN));
System.out.println(Double.isNaN(9.0));
// true
// false

Integer、Byte、Short、Long、Float、Double常用方法

请参阅:Number接口方法Number型包装类通用

Boolean常用方法

praseBoolean、compare、toString、valueOf

虽然Boolean不属于Number型,但是这些方法和Number型是通用的

请参阅:Number接口方法Number型包装类通用

getBoolean

该方法用于判断系统属性字符串是否为"true"

当且仅当系统属性存在,且值等于 “true” 时,才返回 true

public static boolean getBoolean(String name)

1
2
3
4
5
6
7
System.setProperty("s1", "true");
System.setProperty("s2", "false");
System.out.println(Boolean.getBoolean("s1"));
System.out.println(Boolean.getBoolean("s2"));
// 输出结果:
// true
// false

logicalAnd

返回a和b的逻辑与

public static boolean logicalAnd(boolean a, boolean b)

1
2
3
4
5
6
7
8
9
System.out.println(Boolean.logicalAnd(true, true));
System.out.println(Boolean.logicalAnd(true, false));
System.out.println(Boolean.logicalAnd(false, true));
System.out.println(Boolean.logicalAnd(false, false));
// 输出结果
// true
// false
// false
// false

logicalOr

返回a和b的逻辑或

public static boolean logicalOr(boolean a, boolean b)

1
2
3
4
5
6
7
8
9
System.out.println(Boolean.logicalOr(true, true));
System.out.println(Boolean.logicalOr(true, false));
System.out.println(Boolean.logicalOr(false, true));
System.out.println(Boolean.logicalOr(false, false));
// 输出结果
// true
// true
// true
// false

logicalXor

返回a和b的异或

public static boolean logicalXor(boolean a, boolean b)

1
2
3
4
5
6
7
8
9
System.out.println(Boolean.logicalXor(true, true));
System.out.println(Boolean.logicalXor(true, false));
System.out.println(Boolean.logicalXor(false, true));
System.out.println(Boolean.logicalXor(false, false));
// 输出结果:
// false
// true
// true
// false

Character常用方法

Character提供了非常多的字符判断功能,可以实现各种各样的字符判断

isLowerCase

用于判断字符是否为小写

该方法有两种重载形式

  1. public static boolean isLowerCase(char ch)

    判断字符ch是否为小写

  2. public static boolean isLowerCase(int codePoint)

​ 判断Unicode编码对应的字符是否为小写

1
2
3
4
5
6
System.out.println(Character.isLowerCase('z'));
// Unicode码65对应'A'
System.out.println(Character.isLowerCase(65));
// 输出结果:
// true
// false

isUpperCase

用于判断字符是否为大写

该方法有两种重载形式

  1. public static boolean isUpperCase(char ch)

    判断字符ch是否为大写

  2. public static boolean isUpperCase(int codePoint)

​ 判断Unicode编码对应的字符是否为大写

1
2
3
4
5
6
System.out.println(Character.isUpperCase('z'));
// Unicode码65对应'A'
System.out.println(Character.isUpperCase(65));
// 输出结果:
// false
// true

isDigit

用于判断字符是否为数字

该方法有两种重载形式

  1. public static boolean isDigit(char ch)

    判断字符ch是否为数字

  2. public static boolean isDigit(int codePoint)

​ 判断Unicode编码对应的字符是否为数字

1
2
3
4
5
6
System.out.println(Character.isDigit('1'));
// Unicode码65对应'A'
System.out.println(Character.isDigit(65));
// 输出结果:
// true
// false

isLetter

用于判断字符是否为字母

该方法有两种重载形式

  1. public static boolean isLetter(char ch)

    判断字符ch是否为字母

  2. public static boolean isLetter(int codePoint)

​ 判断Unicode编码对应的字符是否为字母

1
2
3
4
5
6
System.out.println(Character.isLetter('1'));
// Unicode码65对应'A'
System.out.println(Character.isLetter(65));
// 输出结果:
// false
// true

isLetterOrDigit

用于判断字符是否为字母或数字

该方法有两种重载形式

  1. public static boolean isLetterOrDigit(char ch)

    判断字符ch是否为字母或数字

  2. public static boolean isLetterOrDigit(int codePoint)

​ 判断Unicode编码对应的字符是否为字母或数字

1
2
3
4
5
6
7
8
System.out.println(Character.isLetterOrDigit('1'));
// Unicode码65对应'A'
System.out.println(Character.isLetterOrDigit(65));
System.out.println(Character.isLetterOrDigit('@'));
// 输出结果:
// true
// true
// false

isWhiteSpace

用于判断字符是否为空格

该方法有两种重载形式

  1. public static boolean isWhiteSpace(char ch)

    判断字符ch是否为空格

  2. public static boolean isWhiteSpace(int codePoint)

​ 判断Unicode编码对应的字符是否为空格

1
2
3
4
5
6
7
8
System.out.println(Character.isWhitespace(' '));
// 空格对应的Unicode为32
System.out.println(Character.isWhitespace(32));
System.out.println(Character.isWhitespace('A'));
// 输出结果:
// true
// true
// false

toUpperCase

将小写字母转换成大写

该方法有两种重载形式

  1. public static char toUpperCase(char ch)

    将字符ch转换成大写

  2. public static int toUpperCase(int codePoint)

    将Unicode编码对应的字符转换成大写,返回大写后的Unicode码

1
2
3
4
5
System.out.println(Character.toUpperCase('c'));
System.out.println(Character.toUpperCase(122));
// 输出结果:
// C
// 90

toLowerCase

将大写字母转换成小写

该方法有两种重载形式

  1. public static char toLowerCase(char ch)

    将字符ch转换成小写

  2. public static int toLowerCase(int codePoint)

​ 将Unicode编码对应的字符转换成小写,返回小写后的Unicode码

1
2
3
4
5
System.out.println(Character.toLowerCase('C'));
System.out.println(Character.toLowerCase(65));
// 输出结果:
// c
// 97