零基础学Java---包装类与String类
包装类 > Java基础入门全套教程
1、什么是包装类?
Java是面向对象的语言,但并不是“纯面向对象”的,因为我们经常用到的基本数据类型就不是对象。但我们在实际应用中经常需要将基本数据转化成对象,以便于操作。
为了解决这个不足,Java在设计类时为每个基本数据类型设计了一个对应的类进行代表,这样八个和基本数据类型对应的类统称为包装类(Wrapper Class)。8种基本数据类型,分别对应一个包装类。包装类均位于java.lang包。
在这八个类名中,除了Integer和Character类以外,其它六个类的类名和基本数据类型一致,只是类名的第一个字母大写而已。
2、为什么需要包装类?
1) 某些方法的参数必须是对象,为了让基本数据类型的数据能作为参数,提供包装类。
2) 包装类还可以提供更多的功能
3) 其他特别重要的功能:比如可以实现字符串和基本数据类型之间的转换
【实例1】认识包装类
public static TestWrapper1{
int num;
Integer in;
public static void main(String[] args){
//1.某些方法参数是对象,为了让基本数据类型能作为参数
List list = new ArrayList();
list.add(new Integer(56));//int 56
list.add(new Integer(100));
list.add(new Double(67.5));
list.add(99);
System.out.println(list);
//2.包装类还可以提供更多的功能
System.out.println(Integer.SIZE);
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.toBinaryString(123));
System.out.println(Integer.toOctalString(123));
System.out.println(Integer.toHexString(123));
//3.特别重要的功能:可以实现字符串和基本数据类型之间的转换
String str = "123";//----123
int num2 = Integer.parseInt(str);
System.out.println(num2);
String str2 = "123.45";
double d = Double.parseDouble(str2);
System.out.println(d);
}
}
注意:
- 注意1:包装类的对象需要占用栈内存和堆内存,而基本数据类型的(局部)变量只占用栈内存;基本数据类型的变量占用空间少,更简单,更灵活,更高效。
- 注意2:作为成员变量,初始值不同。int 0;Integer null。
- 注意3:在这八个类中,除了Character和Boolean以外,其他的都是“数字型“,“数字型”都是java.lang.Number的子类。
3、使用包装类
自动装箱和自动拆箱
自动装箱和拆箱就是将基本数据类型和包装类之间进行自动的互相转换。JDK1.5后,Java引入了自动装箱(autoboxing)/拆箱(unboxing)。
自动装箱:基本类型的数据处于需要对象的环境中时,会自动转为“对象”。
自动拆箱:每当需要一个值时,对象会自动转成基本数据类型,没必要再去显式调用intValue()、doubleValue()等转型方法。
如 Integer i = 5;int j = i; 这样的过程就是自动拆箱。
我们可以用一句话总结自动装箱/拆箱:
自动装箱过程是通过调用包装类的valueOf()方法实现的,而自动拆箱过程是通过调用包装类的 xxxValue()方法实现的(xxx代表对应的基本数据类型,如intValue()、doubleValue()等)。
【示例2】包装类的使用
public class TestWrapper2{
public static void main(String[] args){
//1.自动装箱和自动拆箱
Integer in = 5;
Integer in2 = new Integer(5);//valueOf()
int i = in2;
int i2 = in2.intValue();
//2.==和equals
Integer in3 = new Integer(56);
Integer in4 = new Integer(56);
System.out.println(in3 == in4);//false
System.out.println(in3.equals(in4)); //true
Integer in5 = 25;
Integer in6 = 25;
System.out.println(in5 == in6);
System.out.println(in5.equals(in6)); //true
Integer in7 = 256;
Integer in8 = 256;
System.out.println(in7 == in8);
System.out.println(in7.equals(in8)); //true
}
}
理解Integer源码
Integer的父类是Number类;底层就是封装了一个int类型的value常量,可以通过构造方法、intValue()等赋值取值。
public class Integer extends Number{
private final int value;
public Integer(int value);
this.value = value;
}
@Override
public int intValue(){
return value;
}
}
Integer类提供了一个静态内部类IntegerCache,对于定义一个静态数组cache,长度为256,赋值为-128—127。对于自动装箱时如果是-128—127范围内的数据,直接获取数组的指定值;对于这个范围之外的数据,通过new Integer()重新创建对象。这么做的目的是提高效率。
public class Integer extends Number{
public static java.Integer valueOf(int i){
if((i>=IntegerCache.low)&&(i<=IntegerCache.high)){
return IntegerCache.cache[i+(-IntegerCache.low)];
}
return new java.lang.Integer(i);
}
private static class IntegerCache{
static final int low = -128;
static final int high;
static final java.lang.Integer[] cache;
static{
int h = 127;
high = h;
cache = new java.lang.Integer[(high-low)+1];
int j = low;
for(int k=0;k<cache.length;k++){
cache[k] = new java.lang.Integer(j++);
}
}
private IntegerCache(){
}
}
}
注意:
JDK1.5以后,增加了自动装箱与拆箱功能,如:
Integer i = 100;
int j = new Integer(100);
自动装箱调用的是valueOf()方法,而不是new Integer()方法。
自动拆箱调用的xxxValue()方法。
包装类在自动装箱时为了提高效率,对于-128~127之间的值会进行缓存处理。超过范围后,对象之间不能再使用==进行数值的比较,而是使用equals方法。