Java中的基本类型功能简单,不具备对象的特性,为了使基本类型具备对象的特性,所以出现了包装类,就可以像操作对象一样操作基本类型数据。
基本类型 | 包装器类型 |
boolean | Boolean |
char | Character |
int | Integer |
byte | Byte |
short | Short |
long | Long |
float | Float |
double | Double |
包装类把基本类型数据转换为对象
每个基本类型在java.lang包中都有一个相应的包装类
包装类常用的方法如下:
案例:
public static void main(String[] args) {
//所有包装类都可以将与之对应的基本数据类型作为参数来创建它们的实例对象
Integer a = new Integer(100);
Double b = new Double(100.00);
Character c = new Character('A');
Boolean d = new Boolean(true);
System.out.println(a+" "+ b+" "+c+" "+d);//100 100.0 A true
//除了Character类之外,其他包装类都可以将一个字符串作为参数来构造它们的实例
Integer a1 = new Integer("100");
Double b1 = new Double("100.00");
Boolean d1 = new Boolean("true");
System.out.println(a1+" "+ b1+" "+d1);//100 100.0 true
/*
* Boolean类的构造方法参数为String类型时:
* 若该字符串为true(不论大小写),则该对象表示true,否则表示false
*/
Boolean d2 = new Boolean("True");
Boolean d3 = new Boolean("TRUE");
Boolean d4 = new Boolean("hello");
System.out.println(d2+" "+d3+" "+d4);//true true false
/*
* 当包装类Number构造方法的参数为String类型时,字符串不能为null
* 并且该字符串必须能够解析为基本类型的数据
* 否则会抛出数字格式异常。
*/
Integer a2 = new Integer("");//NumberFormatException: For input string: ""
Integer a3 = new Integer(null);//NumberFormatException: null
Integer a4 = new Integer("abc");//NumberFormatException: For input string: "abc"
}
包装类的作用:
- 集合不允许存放基本数据类型,常用包装类;
- 提供了一系列相关属性和方法,如最大值、最小值、所占位数,类型转换、进制转换等;
private static void show() {
//获取基本数据类型的相关属性
System.out.println(Byte.MIN_VALUE);//-128
System.out.println(Byte.MAX_VALUE);//127
System.out.println(Byte.SIZE);//8
/*
* 包装类型的一些实用操作,如类型转换、进制转换等
* 这里以Integer为例,主要介绍intValue、(static)parseInt、(static)toString等方法
*/
//调用构造器将int类型转换为integer类型,调用intValue方法将integer类型转换为int类型
Integer in1 = new Integer(100);
int in2 = in1.intValue();
//将字符串转换为十进制数的int数,常用于任意进制数转换为十进制数
int num1 = Integer.parseInt("100");
System.out.println(num1); //100
//将字符串按任意进制转换为int数
int num2 = Integer.parseInt("100", 8);
int num3 = Integer.parseInt("ff", 16);
System.out.println(num2); //64
System.out.println(num3);
//将int数转换为对应的String类型的数
String s1 = Integer.toString(100);
System.out.println(s1);//100
//将int数转换为任意进制的String类型的数,常用于十进制数转换为任意进制数
String s2 = Integer.toString(100,8);
String s3 = Integer.toString(100,16);
System.out.println(s2);//144
System.out.println(s3);//64
}
事实上,从JDK1.5就开始引入了自动拆装箱的语法功能,也就是系统将自动进行基本数据类型和与之相对应的包装类型之间的转换,这使得书写代码更加方便。
//自动装箱
int m1 = 190;
Integer num = m1;
System.out.println(num);//190
//自动拆箱
Integer in= new Integer(50);
int number = in;
System.out.println(number);//50
包装类中“==”与equals的用法比较
注意的是,包装类中的equals方法和String类一样,都是重写了Object类中的equals方法,因此比较的是内容而不是地址,而“==”比较的依然是引用变量的地址,只是当包装类型和与之相对应的基本类型进行“==”比较时会先做自动拆箱处理。
private static void show() {
// TODO Auto-generated method stub
Integer a = new Integer(-100);
Integer b = new Integer("-100");
int c = -100;
System.out.println(a == b);//false
System.out.println(a == c);//true
System.out.println(b == c);//true
System.out.println(a.equals(b));//true
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);//false
System.out.println(s1.equals(s2));//true
}
注意事项:
(1)Boolean类构造方法参数为String类型时,若该字符串内容为 true(不考虑大小写),则该Boolean对象表示true,否则表示false。
(2)当Number包装类构造方法参数为String 类型时,字符串不能为null,
且该字符串必须可解析为相应的基本数据类型的数据,否则编译通过,运行时NumberFormatException异常。