Java包装类、自动拆装箱机制

Java包装类、自动拆装箱

复习一下Java的基本数据类型、包装类、自动装/拆箱机制,以及一些面试的坑。

基本数据类型及其包装类

在JAVA中一共有八种基本数据类型,他们分别是
byte、short、int、long、float、double、char、boolean 。

基本类型功能简单,不具备对象的特性,为了使基本类型具备对象的特性,所以出现了包装类,就可以像操作对象一样操作基本类型数据。

常用的包装类可以分为三类:Character、Number、Boolean。

Number类是所有数字型包装类的父类。

基本类型与包装类的对应关系如下:

byteshortintlongfloatdoublecharboolean
位数81632643264161
字节数12484821(1/8)
默认值0000L0.0f0.0dfalse
包装类型ByteShortIntegerLongFloatDoubleCharacterBoolean

装/拆箱

Java提供的包装类可以操作基本数据类型,最常用的是类型转换。

JDK1.5就开始引入了自动拆装箱的语法功能,也就是系统将自动进行基本数据类型和与之相对应的包装类型之间的转换,这使得程序员书写代码更加方便。

包装类的基本方法

Number类:

1	xxxValue()
将 number 对象转换为xxx数据类型的值并返回。
2	compareTo()
将number对象与参数比较。
3	equals()
判断number对象是否与参数相等。
4	valueOf()
返回一个 Number 对象指定的内置数据类型
5	toString()
以字符串形式返回值,Number类型方法,不是Object类的方法。
6	parseInt()
将字符串解析为int类型。
包装类的一些性质
  • 所有包装类都可以将与之对应的基本数据类型作为参数来创建它们的实例对象
  • 除了Character类之外,其他包装类都可以将一个字符串作为参数来构造它们的实例
  • Boolean类的构造方法参数为String类型时,若该字符串为true(不论大小写),则该对象表示true,否则表示false
  • 当包装类Number构造方法的参数为String类型时,字符串不能为null,并且该字符串必须能够解析为基本类型的数据

自动装/拆箱如:

	Integer integer=18;
    System.out.println(integer);

//18是基本int类型,赋给其包装类就是装箱


	Integer integer2=new Integer("19");//字符串作为参数来构造实例
    int i=integer2;
//integer2是对象,将其赋给int型就是拆箱

装箱过程是通过调用包装器的valueOf方法实现的,而拆箱过程是通过调用包装器对象的 xxxValue方法实现的。

valueOf方法为static方法。

		
		int s1=Integer.parseInt("100");	//字符串被解析成int	
        System.out.println(s);//100

        Integer s2= Integer.valueOf(1);//int包装成Integer
        System.out.println(s2);//1

		//字符串先被parseInt(String s)方法解析为int型,再被valueOf(int i)方法装箱
        Integer s3=Integer.valueOf("1");
        System.out.println(s3);//1


		Integer integer=18;//装箱
        int i=integer.intValue();//拆箱
        System.out.println(i);

包装器的public static String toString(int i)方法可以方便的将Number类型转换成String类型。

		Integer integer=18;//装箱
        String s=Integer.toString(integer);//先拆箱再转换
        System.out.println(s);

String和包装器转换

toString和 parsexxx方法能够将基本类型和String类型互相转换,再通过拆装箱就可以实现包装器和String的互相转换。

public static String toString(int i)
public static int parseInt(String s)
测试

System.out.println(String s)是我们常用的方法,将Integer对象与字符串连接打印。

	Integer i=100;//装箱
    System.out.println("hello "+i);//先拆箱再解析成String类型   

这里100被包装成Integer对象i,与字符串"hello "拼接时被拆成基本类型int,之后int类型又被自动转换成String类型。

面试题

经常看到面试题中对取值范围及是否同一对象进行考察,这部分也记下来。

equals方法用于比较数据内容、==不仅比较数据内容还比较是否为同一对象。

单纯的比较数据和对象很容易。

	   int i1=97;
       int i2=97;
       int i3=197;
       int i4=197;
       
       //数据相同
       System.out.println(i1==i2);//true
       System.out.println(i3==i4);//true
     
		
	   Integer integer1=new Integer(97);
       Integer integer2=new Integer(97);
       Integer integer3=new Integer(197);
       Integer integer4=new Integer(197);

     	//数据相同但不是同一个对象
       System.out.println(integer1.equals(integer2));//true
       System.out.println(integer1==integer2);//false
       System.out.println(integer3.equals(integer4));//true
       System.out.println(integer3==integer4);//false

结合拆装箱的时候、有些东西就要注意了。

	   Integer l1=97;
       Integer l2=97;
       Integer l3=197;
       Integer l4=197;

          
       System.out.println(l1.equals(l2));//true
       System.out.println(l1==l2);//true
       System.out.println(l3.equals(l4));//true
       System.out.println(l3==l4);//false

作为常量时,对于-128到127之间的数,会进行缓存,当超过范围就是new一个对象了.

       Double l1=10000000000000000000000000000000000000000000000000000000000000d;
       Double l2=10000000000000000000000000000000000000000000000000000000000000d;
       Double l3=10d;
       Double l4=10d;
        
       System.out.println(l1.equals(l2));//true
       System.out.println(l1==l2);//false
       System.out.println(l3.equals(l4));//true
       System.out.println(l3==l4);//false
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值