学习内容:包装类(其中例子均以int类型为例,其他类型照葫芦画瓢)
在Java5 中添加了两个新特性,那就是自动装箱和拆箱,因为基本类型的广泛使用,但是Java 又是面向对象的语言,所以提供了包装类型的支持。
包装类分别是:Byte(byte), Short(short), Integer(int), Long(long), Float(float), Double(double), Character(char), Boolean(boolean)八种(括号里面是它们分别对应的基本数据类型)。
那么为什么要再提供8种包装类呢?
JAVA是面向对象的语言,很多类和方法中的参数都需使用对象(例如集合),但基本数据类型却不是面向对象的,这就造成了很多不便。如:List<int> = new ArrayList<>();
,就无法编译通过。为了解决该问题,我们引入了包装类,顾名思义,就是将基本类型“包装起来“,使其具备对象的性质,包括可以添加属性和方法,位于java.lang包下。
装箱和拆箱
装箱:将基本数据类型转换为对应包装类的引用数据类型的过程叫“装箱”。
Integer i =new Integer(123);
拆箱:将包装类的引用数据类型转换为对应的基本数据类型的过程叫“拆箱”
float f = i.floatValue();
包装类中常用的方法
八种包装类中:Byte、Short、Integer、Long、Float、Double的父类是Number。Character和Boolean的父类是Object。
Number是一个抽象类,无法实例化对象。其中的方法有:
byte byteValue()
以byte形式返回指定的数值。
abstract double doubleValue()
以double 形式返回指定的数值。
abstract float floatValue()
以float 形式返回指定的数值。
abstract int intValue()
以int形式返回指定的数值。
abstract long longValue()
以long 形式返回指定的数值。
short shortValue()
以short 形式返回指定的数值。
Integer包装类中的构造方法: Integer(int value)
和Integer(String s)
(注意:Integer(String s)
里面传的参数只能是String 类型的数字对象)
public class Test01 {
public static void main(String[] args) {
Integer i =new Integer(123);
System.out.println(i);//输出结果123
Integer j =new Integer("123");
System.out.println(j);//输出结果123
//Integer k =new Integer("中国"); 报错,"中国"不是数字,没法包装成Integer
Double x=new Double(123);
System.out.println(x);//输出结果123.0
Double y =new Double("123");
System.out.println(y);//输出结果123.0
}
}
Integer包装类中的常量:MAX_VALUE
、MIN_VALUE
。
public class Test01 {
public static void main(String[] args) {
System.out.println("int的最大值"+Integer.MAX_VALUE);//输出结果:int的最大值2147483647
System.out.println("int的最小值"+Integer.MIN_VALUE);//输出结果:int的最小值-2147483648
System.out.println("byte的最大值"+Byte.MAX_VALUE);//输出结果:byte的最大值127
System.out.println("byte的最小值"+Byte.MIN_VALUE);//输出结果:byte的最小值-128
}
}
Integer包装类中常用的方法:
static int parseInt(String s)
将字符串转换成int类型
public class Test01 {
public static void main(String[] args) {
int x =Integer.parseInt("123");
System.out.println(x);//输出结果123
double y=Double.parseDouble("123");
System.out.println(y);//输出结果123.0
}
}
static String toBinaryString(int i)
将十进制转换成二进制字符串。static String toHexString(int i)
将十进制转换成十六进制字符串。static String toOctalString(int i)
将十进制转换成八进制字符串。
public class Test01 {
public static void main(String[] args) {
//将十进制转换成二进制字符串
String x = Integer.toBinaryString(3);
System.out.println(x);//输出结果:11
//将十进制转换成十六进制字符串
String y =Integer.toHexString(17);
System.out.println(y);//输出结果:11
//将十进制转换成八进制字符串
String z=Integer.toOctalString(10);
System.out.println(z);//输出结果:12
}
}
static Integer valueOf(int i)
将int类型转换成Integerstatic Integer valueOf(String s)
将String类型转换成Integer
public class Test01 {
public static void main(String[] args) {
//int--->Integer
Integer i1=Integer.valueOf(100);
System.out.println(i1);//输出结果:100
//String--->Integer
Integer i2=Integer.valueOf("100");
System.out.println(i2);//输出结果:100
}
}
自动装箱和自动拆箱
在jdk1.5之后的版本,就开始支持自动装箱和自动拆箱了。
自动装箱:基本数据类型自动转换成包装类型。
自动拆箱:包装类型自动转换成基本数据类型。
public class Test01 {
public static void main(String[] args) {
//自动装箱
//这里并不是将一个基本数据类型直接赋值给引用数据类型,
//代码原理还是Integer i =new Integer(100);
//只是后面new Integer对象不用我们手动输入了。
Integer i=100;
//自动拆箱
//原理和自动装箱一样
int j=i;
}
}
思考:
public class Test01 {
public static void main(String[] args) {
Integer a=1000;//实际是Integer a =new Integer(1000);
Integer b=1000;//实际是Integer b =new Integer(1000);
System.out.println(a==b);//输出结果为false
}
}
输出结果之所以是false是因为“==”比较的两个引用的内存地址,而a和b实际上都是一个Integer对象的引用,所以比较的是a和b的内存地址,因为自动装箱实际上还是new了对象,所以a和b的内存地址不一样,输出false。
public class Test01 {
public static void main(String[] args) {
Integer x=127;
Integer y=127;
System.out.println(x==y);//输出结果为true
}
}
按照上面的解释,这里应该输出false才对,为什么会输出true呢?
解释:java中为了提高程序的执行效率,将[-128,127]之间左右的包装对象提前创建好,放到了应该方法区的“整数型常量池”当中了,目的是只要用这个区间的数据不需要在new了,直接从整数型常量池当中取出来。
x变量中保存的对象内存地址和y变量中保存的对象内存地址是一样的。
String、int、Integer 类型的互相转换
例:
public class Test01 {
public static void main(String[] args) {
//String--->int
int i1=Integer.parseInt("100");//i1是100数字
System.out.println(i1+1);//输出结果:101
//int--->String
String s2=i1+"";//“100”字符串
System.out.println(s2+1);//"1001"
//int--->Integer
Integer x=1000;//自动装箱
//Integer--->int
int y=x;//自动拆箱
//String--->Integer
Integer k=Integer.valueOf("123");
//Integer--->String
String e=String.valueOf(k);
}
}