包装类 (warpper)
八中基本类型相对应的引用类型——包装类
有了类的特性,就可以调用方法
当基本数据类型==引用数据类型(包装类),判断的是值是否等
包装类判断值是否相等时必须使用equls判断
| 基本数据类型 | 包装类 |
|---|---|
| bety | Bety |
| int | Integer |
| char | Character |
| float | Float |
| double | Double |
| short | Short |
| boolean | Boolean |
| long | Long |
装修拆箱
class test{
public static void main(String[] args){
//jdk5之前,需要手动装箱和拆箱
//手动装箱 int——>Integer
int n1 = 100;
Integer i1 = new Integer(n1);//源码是调用ValueOf()方法
Integer i1 = Integer.ValueOf(n1);//上下二者其一均可
//手动拆箱 Integer——>int
int n2 = Integer.intValue(i1);
//————————————————————————————————————————————————————————————————————
//jdk5以后,自动装箱和拆箱
//自动装箱 int——>Integer
int n1 = 100;
Integer i1 = n1;
//自动拆箱 Integer——>int
int n2 = i1;
//前后底层都相同
}
}
常用方法
class test{
public static void main(String[] args){
//包装类(Integer)——>String
Integer i1 = 10;
//方式一
String str1 = i1.toString();
//方式二
String str2 = i1 + "";
//方式二
String str3 = String.ValueOf(i1);
//String——>包装类(Integer)
String str = "123"
//方式一
Integer i2 = String.parseInt(str);//自动装箱
//方式二
Integer i3 = new Integer(str);//使用Integer的构造器
}
}
一些常用方法