登录  /  注册
首页 > Java > java教程 > 正文

总结Java中的Integer和int的区别详细解析

php是最好的语言
发布: 2018-08-06 16:37:18
原创
2164人浏览过

上面的一篇提到过,为什么Java泛型为什么用对象而不是原始类型?但是对Integer和int这两个的区别还是不怎么懂,就继续百度了一下,找到了一篇大佬的文章,感觉还是不错的,就转载分享一下。

integer和int的区别

Integer是int提供的封装类,而int是Java的基本数据类型; Integer默认值是null,而int默认值是0; 声明为Integer的变量需要实例化,而声明为int的变量不需要实例化; Integer是对象,用一个引用指向这个对象,而int是基本类型,直接存储数值。

除了知道Integer和int最基本的区别外,还需要了解一点其他关于Integer和int实际使用中的回遇到的问题。
否则如果面试官再问一下Integer i = 1;int ii = 1; i==ii为true还是为false?估计就有一部分人答不出来了,如果再问一下其他的,估计更多的人会头脑一片混乱。所以我对它们进行了总结,希望对大家有帮助。
代码如下:

public class Main {
 
    public static void main(String[] args) {
 
        int a = 128;
        Integer b = 127;
        Integer c = 127;
        Integer d = 128;
        Integer e = 128;
        Integer f = new Integer("128");
        Integer g = new Integer("127");
        Integer h = new Integer("127");
 
 
        //方案一()
        System.out.println("方案一:Integer与new Integer不同初始化方法的值的比较,Integer与new Integer不会相等");
        //Integer e = 128;
        //Integer f = new Integer("128");
        if (e == f){
            System.out.println("true");
        }else{
            System.out.println("false");
        }
        System.out.println("---------------------");
 
        //方案二
        System.out.println("方案二:创建两个都是直接赋值的Integer");
        //Integer b = 127;
        //Integer c = 127;
        if (b == c){
            System.out.println("true");
        }else{
            System.out.println("false");
        }
        //Integer d = 128;
        //Integer e = 128;
        if (d == e){
            System.out.println("true");
        }else{
            System.out.println("false");
        }
        System.out.println("---------------------");
 
        //方案三
        System.out.println("方案三:两个都是new出来的Integer");
        //Integer g = new Integer("127");
        //Integer h = new Integer("127");
        if (g == h){
            System.out.println("true");
        }else{
            System.out.println("false");
        }
        System.out.println("---------------------");
 
        //方案四
        System.out.println("方案四:int与Integer比较");
        //int a = 128;
        //Integer f = new Integer("128");
        if (a == f){
            System.out.println("true");
        }else{
            System.out.println("false");
        }
 
    }
}
登录后复制

运行结果如下:

方案一:Integer与new Integer不同初始化方法的值的比较,Integer与new Integer不会相等
false
---------------------
方案二:创建两个都是直接赋值的Integer
true
false
---------------------
方案三:两个都是new出来的Integer
false
---------------------
方案四:int与Integer比较
true
登录后复制

总结如下:

方案一:
我先对e、f这两个对象,使用Integer和new Integer这两个不同的初始化方法,在对这两个对象进行比较;

Integer e = 128;
Integer f = new Integer("128");
 
if (e == f){
    System.out.println("true");
}else{
    System.out.println("false");
}
登录后复制

返回结果:false
得出结论:Integer与new Integer不会相等。e的引用指向堆,而f指向专门存放他的内存(常量池),他们的内存地址不一样,所以为false

方案二:
创建两个都是直接赋值的Integer,在对这两个对象进行比较;

//Integer b = 127;
//Integer c = 127;
if (b == c){
    System.out.println("true");
}else{
    System.out.println("false");
}
//Integer d = 128;
//Integer e = 128;
if (d == e){
    System.out.println("true");
}else{
    System.out.println("false");
}
登录后复制

返回结果:
true
false
得出结论:两个都是直接赋值的Integer,如果数在-128到127之间,则是true,否则为false
原因: java在编译Integer i2 = 128的时候,被翻译成-> Integer i2 = Integer.valueOf(128);而valueOf()函数会对-128到127之间的数进行缓存

看一下源码大家都会明白,对于-128到127之间的数,会进行缓存,Integer b = 127时,会将127进行缓存,下次再写Integer c = 127时,就会直接从缓存中取,就不会new了。

/**
     * Cache to support the object identity semantics of autoboxing for values between
     * -128 and 127 (inclusive) as required by JLS.
     *
     * The cache is initialized on first usage.  The size of the cache
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
     * During VM initialization, java.lang.Integer.IntegerCache.high property
     * may be set and saved in the private system properties in the
     * sun.misc.VM class.
     */
 
    private static class IntegerCache {
        static final int low = -128;
        static final int high;
        static final Integer cache[];
 
        static {
            // high value may be configured by property
            int h = 127;
            String integerCacheHighPropValue =
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
            if (integerCacheHighPropValue != null) {
                try {
                    int i = parseInt(integerCacheHighPropValue);
                    i = Math.max(i, 127);
                    // Maximum array size is Integer.MAX_VALUE
                    h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
                } catch( NumberFormatException nfe) {
                    // If the property cannot be parsed into an int, ignore it.
                }
            }
            high = h;
 
            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
 
            // range [-128, 127] must be interned (JLS7 5.1.7)
            assert IntegerCache.high >= 127;
        }
 
        private IntegerCache() {}
    }
登录后复制

方案三:
我先对g、h这两个对象,使用new Integer初始化方法,在对这两个对象进行比较;

Integer g = new Integer("127");
Integer h = new Integer("127");
if (g == h){
    System.out.println("true");
}else{
    System.out.println("false");
}
登录后复制

返回结果:false
得出结论:两个都是new出来的Integer对象,指向不同的两个Integer对象,都为false

方案四:
我先对a、f这两个对象,使用int和new Integer这两个不同的初始化方法,在对这两个对象进行比较;

int a = 128;
Integer f = new Integer("128");
if (a == f){
    System.out.println("true");
}else{
    System.out.println("false");
}
登录后复制

返回结果:true
得出结论:int和integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比

相关文章:

Java教程—int与Integer的区别

Java中关于int和Integer的区别详解

以上就是总结Java中的Integer和int的区别详细解析的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号