码迷,mamicode.com
首页 > 其他好文 > 详细

int和Integer之间的区别和联系

时间:2017-02-18 15:25:52      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:eof   static   操作   发行版   intvalue   编译   包含   url   创建   

      在工作中使用==埋下的坑这篇博文中,我们看到当使用基本类型的时候==是完全没有问题的,部分或者混合使用基本类型和装箱基本类型的时候,就可能出现问题了,那么我们可能会想基本类型和装箱基本类型有什么区别和联系哪?下面以int和Integer为例讲讲我的看法。int和Integer非常的像,所有的基本类型和其对应的装箱基本类型都非常的像,但是他们之间也是有区别的,他们之间的区别是什么哪?

为了明白他们之间的区别和联系,我们一起看看下面这段简单的代码和罗织在代码之间的结论吧!

1:TestIntMain.java——包含基本类型和装箱基本类型的区别和联系的结论注释

import java.util.ArrayList;
import java.util.List;

public class TestIntMain {
    public static void main(String [] args){

        /**
         * 区别1:
         * int 声明的变量不能直接赋值为null
         * Integer 声明的变量能直接赋值为null
         */
//        int _int_ = null;//不能直接赋null
        Integer _Integer_ = null;//可以直接赋null

        /**
         * 区别2:
         * int 声明的变量,存储就是一个数值,不能调用方法,它也没有方法可调用
         * Integer 声明的变量,是一个对象,它有对应的变量、方法,它可以自由的调用
         */
        int _int = 99;
        Integer _Integer1 = new Integer(99);
        //一个整数值,不能调用方法
//        System.out.println("_int type is : "+_int.getClass().getSimpleName()+"_int value is : "+_int.toString());
        System.out.println("_int  value is : "+_int);

        //一个对象,可以调用它的方法,属性等等
        System.out.println("_Integer1 type is : "+_Integer1.getClass().getName()+"   _Integer1 value is : "+_Integer1);

        /**
         * 自动装箱和拆箱
         * 自动装箱:我的理解是将基本类型的变量,自动转换为装箱基本类型的对象的过程
         * 自动拆箱:我的理解是将装箱基本类型的对象,自动转换为基本类型的变量的过程
         * 1:这是在Java1.5发行版本中增加的功能
         * 2:目的是为了模糊基本类型和装箱基本类型之间的区别,减少使用装箱基本类型的繁琐性
         * 3:这样做也有一定的风险,比如:对于装箱基本类型运用==操作符几乎总是错误的,装箱的操作会导致高的开销和不必要的对象创建
         */
        int _int1 = new Integer(99);//对应的匿名对象变成了一个整数值,自动拆箱
        int _int2 = _Integer1;//_Integer1对象变成了一个整数值,自动拆箱
        //        System.out.println("_int1 type is : "+_int1.getClass().getSimpleName()+"_int1 value is : "+_int1.toString());
        System.out.println("_int1 value is : "+_int1);

        Integer _Integer = 99;//对应的整数值,直接给你对象也没问题,自动装箱
        System.out.println("_Integer  type is : "+_Integer.getClass().getName()+"   _Integer  value is : "+_Integer);

//        List<int> list_ = new ArrayList<int>();
        List<Integer> list = new ArrayList<Integer>();
        list.add(_int);//_int 此时相当于是一个Integer类型的对象了,自动装箱
        list.add(_Integer);
    }
}

 

 1:TestIntMain.class——包含基本类型和装箱基本类型的自动装箱和拆箱的实现方式

import java.util.ArrayList;

public class TestIntMain {
    public TestIntMain() {
    }

    public static void main(String[] args) {
        Object _Integer_ = null;//Integer _Integer_ = null; 编译后是这样的哦
        byte _int = 99;//int _int = 99; 编译后是这样的哦
        Integer _Integer1 = new Integer(99);
        System.out.println("_int  value is : " + _int);
        System.out.println("_Integer1 type is : " + _Integer1.getClass().getName() + "   _Integer1 value is : " + _Integer1);
        int _int1 = (new Integer(99)).intValue();//自动拆箱的实现方式
        int _int2 = _Integer1.intValue();//自动拆箱的实现方式
        System.out.println("_int1 value is : " + _int1);
        Integer _Integer = Integer.valueOf(99);//自动装箱的实现方式
        System.out.println("_Integer  type is : " + _Integer.getClass().getName() + "   _Integer  value is : " + _Integer);
        ArrayList list = new ArrayList();
        list.add(Integer.valueOf(_int));//自动装箱的实现方式
        list.add(_Integer);
    }
}

 

int和Integer之间的区别和联系

标签:eof   static   操作   发行版   intvalue   编译   包含   url   创建   

原文地址:http://www.cnblogs.com/godtrue/p/6413066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!