目录
1.1.每一个基本类型都有与之对应的对象(包装类型),所谓的面向对象编程!操作对象一定比操作基本类型简单因为对象提供了强大的功能供我们使用。
一、包装类Wrapper
1.1.每一个基本类型都有与之对应的对象(包装类型),所谓的面向对象编程!操作对象一定比操作基本类型简单因为对象提供了强大的功能供我们使用。
1.2.java一共有8种基本数据类型,分别为:byte、short、int、long、float、double、char、boolea。其对应的对象(包装类型)分别为:Byte、Short、Integer、Long、Float、Double、Character、Boolean。
二、转换关系
2.1.转换关系:
基本类型-> 包装类 Integer.valueOf(int i) 包装类-> 基本类型 x.intValue() 基本类型 -> String String.valueOf(String s) String ->基本类型 Integer.parseInt(Sting s) 包装类 ->String integer2.toString(); String -> 包装类 Integer.valueOf(s2);
2.2自动装箱和拆箱(autoboxing)
三、代码示例
public class TestWrapper {
// 主方法:程序的入口 -> 读代码的入口,看一个代码从main的第一行开始看起。
public static void main(String[] args) {
// 1.基本类型 -> 包装类
int i =100;
Integer integer = Integer.valueOf(i);
System.out.println(integer);
// 2.包装类 -> 基本类型
Integer integer1 =100;
int i1 =integer1.intValue();
System.out.println(i1);
// 3.基本类型 -> String
String s = String.valueOf(i);
System.out.println(s);
//!!!4.String -> 基本类型
String s1 ="123";
int i2 = Integer.parseInt(s1);
System.out.println(i2);
//5. String -> 包装类
String s2 ="12333";
Integer integer3 = Integer.valueOf(s2);
System.out.println(integer3);
//6. 包装类 -> String
Integer integer2 =1233;
String s3 = integer2.toString();
System.out.println(s3);
//7.autoboxing:基本类型和包装类之间的转换的自动完成的。
//直接写出来的数字默认为int,直接写出来的小数默认为double。
Integer x =100; // Integer .valueof(100);自动装箱
Integer y =200; // Integer .valueof(200);自动装箱
/*
int x = x.intValue();//自动拆箱
int y = y.intValue();//自动拆箱
int z =x+y;
Integer z = Integer.valueOf(z);//自动装箱
* */
Integer z =x+y;//
System.out.println(z);//300
}
}