包装类
1.基本类型和包装类型
基本类型 | byte | short | int | long | float | double | char | boolean |
---|---|---|---|---|---|---|---|---|
包装类型 | Byte | Short | Integer | Long | Float | Double | Char | Boolean |
2.装箱和拆箱
装箱:把基本类型的数据,包装到包装类中(基本类型的数据 -->包装类)
构造方法:
Integer(int value)构造一个新分配的Integer对象,他表示指定的 int值
Integer(String s) 构造一个新分配的Integer对象,他表示String参数所指示的int值传递的字符串,必须是基本类型的字符串,否则会抛出异常,“100”正确,“a”错误。
静态方法:
static Integer valueOf( int i ) 返回一个表示指定的int值的Integer实例
static Integer valueOf(String s)返回保存指定的String的值得Integer对象
拆箱:在包装类中取出基本类型的数据(包装类 -->基本类型的数据
成员方法:
int intvalue( ) 以int类型返回该Integer的值
public static void main(String[] args) {
//装箱
Integer integer = new Integer(1);//已过时 1
System.out.println(integer);
Integer integer1 = new Integer("1");//已过时 1
System.out.println(integer1);
//静态方法
Integer integer2 = Integer.valueOf(5);
System.out.println(integer2);//5
// Integer integer3 = new Integer.valueOf("a");//错误
Integer integer3 = Integer.valueOf("3");
System.out.println(integer3);//3
//拆箱
int i = integer.intValue();
System.out.println(i);//1
}
自动装箱与自动拆箱:基本类型和包装类之间可以自动的相互转换,JDK1.5之后出现的新特性
自动装箱:直接把int类型的整数赋值给包装类,Integer in = 1;
相当于Integer in = new Integer(1);
Integer in = 1;
System.out.println(in);
/*
自动拆箱:in是包装类,无法直接参与运算,可以自动转换为基本类型的数据,再参与计算
*/
in = in + 2;//相当于in = in.intValue()+2
System.out.println(in);//3
boolean isInstance = in instanceof Integer;//in 仍然为包装类
System.out.println(isInstance);//true
ArrayList集合无法直接存储整数,可以存储Integer包装类
ArrayList<Integer> list = new ArrayList<>();
list.add(1);//自动装箱 相当于list.add(new Integer(1))
int a = list.get(0);//自动拆箱 相当于list.get().intValue()
可以看到没有自动装箱,自动拆箱会麻烦很多
3.基本数据类型和字符串之间的转换
一、将数字转换为字符串
1.基本类型直接与“” 相连接即可
2.使用包装类中的静态方法
static String toString(int i)返回一个表示指定整数的String对象
3.使用String类中的静态方法
static String valueOf(int i) 返回int 参数的字符串表示形式
public class Demo02WrapperTranslate {
public static void main(String[] args) {
//方法一 和字符串相加
String s = 10 + "";
System.out.println(s + 10);
//方法二
String s1 = Integer.toString(100);
String s2 = Character.toString('a');
System.out.println(s1 + 200);
System.out.println(s2 + 12);
//String.valueOf()
String s3 = Integer.toString(100);
System.out.println(s3);
}
}
二、String 转换成对应的的基本类型
除了Character类之外,其他所有包装类都具有parse竟然i方法发可以将字符串参数转换为对应的基本类型:
-
public static byte parseByte(String s) : 将字符串参数转换为对应的byte基本类型
-
public static short parseShort(String s) : 将字符串参数转换为对应的short基本类型
-
public static int parseInteger(String s) : 将字符串参数转换为对应的int基本类型
-
public static long parseLong(String s) : 将字符串参数转换为对应的long基本类型
-
public static float parseFloat(String s) : 将字符串参数转换为对应的float基本类型
-
public static double parseDouble(String s) : 将字符串参数转换为对应的double基本类型
-
public static boolean parseBoolean(String s) : 将字符串参数转换为对应的boolean基本类型
代码使用(仅以Integer类的parseXxxx方法为例)
public class Demo02WrapperParse {
public static void main(String[] args) {
int num = Integer.parseInt("100");
System.out.println(num);//100
/*int num = Integer.parseInt("100a");
System.out.println(num);//error*/
//字符串转换为Longxing
Long a = Long.parseLong("100");
System.out.println(a);//100
boolean isInstance = a instanceof Long;// l 仍然为包装类
System.out.println(isInstance);//true
}
注意:若字符串不能转换为对应的基本类型,会抛出异常(数字格式化异常)
java.lang.NumberFormatException: For input string: “100a”