Java语言提供了八种基本类型: 六种数字类型(四个整数型,两个浮点型),一种字符类型,还有一种布尔型 基本数据类型不具有对象特征,没有属性、方法、无法对象化交互
通过包装类,可以让基本数据类型获取对象一样的特征,行使对象的相关权益Java中的包装类包括: Integer、Long、Short、Byte、Character、Double、Float、Boolean、BigInteger、BigDecimal其中 BigInteger、BigDecimal 没有相对应的基本类型,主要应用于高精度的运算 BigInteger 支持任意精度的整数BigDecimal 支持任意精度带小数点的运算
在Java中,一切皆对象,但八大基本类型却不是对象声明方式的不同 基本类型无需通过 new 关键字来创建包装类型需 new 关键字 存储方式及位置不同 基本类型是直接存储变量的值保存在栈中能高效的存取包装类型需要通过引用指向实例,具体的实例保存在堆中 初始值的不同 包装类型的初始值为 null基本类型的初始值视具体的类型而定,比如 int 类型的初始值为0,boolean 类型为 false 使用方式的不同 与集合类合作使用时只能使用包装类型
JAVA 是一种面向对象语言,JAVA 中的类把方法与数据连接在一起,构成了自包含式的处理单元在 JAVA 中不能定义基本类型(primitive type)对象,为了能将基本类型视为对象进行处理,并能连接相关的方法,JAVA 为每个基本类型都提供了包装类如: int 型数值的包装类 Integerboolean 型数值的包装类 Boolean 等 这样便可以把这些基本类型转换为对象来处理了
装箱:把基本数据类型转换成包装类 自动装箱手动装箱 拆箱:把包装类转换成基本数据类型 自动拆箱手动拆箱 案例: public class HelloWorld { public static void main(String[] args) { // 定义int类型变量,值为100 int score = 100; // 创建Integer包装类对象,表示变量score1的值 //自动装箱 Integer score1=score; // 将Integer包装类转换为double类型 double score2=score1.doubleValue(); // 将Integer包装类转换为long类型 long score3=score1.longValue(); // 将Integer包装类转换为int类型 int score4=score1.intValue(); //打印输出 System.out.println("score对应的Integer类型结果为:"+score1); System.out.println("score对应的double类型结果为:"+score2); System.out.println("score对应的long类型结果为:"+score3); System.out.println("score对应的int类型结果为:"+score4); } }
基本数据类型转换为字符串 使用包装类的 toString() 方法 字符串转换为基本数据类型 自动拆箱调用包装类的 parseXxx() 静态方法调用包装类的 valueOf() 方法转换为基本类型的包装类,自动拆箱 案例: public class StringUse { public static void main(String[] args) { double a = 12.5; //将基本类型转换为字符串 String str1 = Double.toString(a); System.out.println("a 转换为String型后+10的结果为: "+ (str1+10)); String str = "2.8"; // 将字符串转换为基本类型 //double b = Double.valueOf(str); double b = Double.parseDouble(str); System.out.println("str 转换为double型后+10的结果为: "+ (b+10)); } }
案例: public class WrapperTest { public static void main(String[] args) { // TODO Auto-generated method stub Integer one=new Integer(100); Integer two=new Integer(100); System.out.println("one==two的结果:"+(one==two));//1 Integer three=100;//自动装箱 //Integer three=Integer.valueOf(100); System.out.println("three==100的结果:"+(three==100));//2 自动拆箱 //Integer four=100; Integer four=Integer.valueOf(100); System.out.println("three==four的结果:"+(three==four));//3 Integer five=200; System.out.println("five==200的结果:"+(five==200));//4 Integer six=200; System.out.println("five==six的结果:"+(five==six));//5 Double d1=Double.valueOf(100); System.out.println("d1==100的结果:"+(d1==100)); Double d2=Double.valueOf(100); System.out.println("d1==d2的结果:"+(d1==d2)); } } 当方法 valueOf() 在执行的过程中,如果传进来的参数范围大于等于 -128 且小于等于 127 时,会直接从对象池中查找是否存在这样一个对象 如果有这样的一个对象,将会直接产生如果没有这样的一个对象,将会隐式调用 new 关键字,实例化一个对象 Java 中八种基本面数据类型及其对应包装类,除了 float 和 double,其他的都可以应用对象常量池概念
public class HelloWorld { public static void main(String[] args) { //定义float类型变量,赋值为88.99 float f1 =88.99f ; //将基本类型转换为字符串 String s=Float.toString(f1)+20; //打印输出 System.out.println("f1转换为String型后与整数20的求和结果为:"+s); //定义String类型变量,赋值为"188.55" String str ="188.55"; // 将字符串转换为基本类型double double d=Double.valueOf(str)+20; //打印输出 System.out.println("str转换为double型后与整数20的求和结果为:"+d); } }