zoukankan      html  css  js  c++  java
  • Integer与int的区别

    int是一种基本数据类型,Integer是其的包装类,下面我们提出几个问题:

      问题一:既然有了包装类,为什么要有基本数据类型?

      答:其实完全可以没有,但是这样我们使用起来就不方便了,因此基本数据类型相当于是java给我们留下的语法糖。Integer属于引用类型,new一个对象存储在堆里,我们通过栈中的引用来使用这些对象;但是对于经常用到的一系列类型如int,如果我们用new将其存储在堆里就不是很有效(特别是简单的小的变量),所以就出现了基本类型,同C++一样,Java采用了相似的做法,对于这些类型不是用new关键字来创建,而是直接将变量的值存储在栈中,更加高效。

      问题二:既然有了基本数据类型,为什么还要有其封装类?(问题一、二可以合并成一个问题)

      我们知道java是面向对象的语言,基本数据类型并不具有对象的性质,为了使其具有对象的性质,就出现了包装类,并添加了一系列属性和方法,丰富了操作。比如,类型转换,int i = 0;String s = Integer.toString(i);等等。而且在使用集合类型List、Map等时就一定要使用包装类型而非基本类型,int、double等基本类型是放不进去的,因为容器都是填装的Object。

      问题三:两者有什么具体的不同点?

      答:1,两者的转换,这里有两个名词叫做自动装箱和自动拆箱。自动装箱是将java基本数据类型自动转换成对应的包装类型,自动拆箱是将包装类型自动转换成基本数据类型。

            int i = new Integer(99);//自动拆箱,实际执行的是int i = new Integer(99).intValue();
            Integer j  = 99;//自动装箱,实际执行的是Integer j = Integer.valueOf(99);

      2,两者的比较,首先是int与Integer的比较,此时是将Integer转成int类型,然后再比较两个值是否相等。

            int m = 200;
            Integer n = new Integer(200);
            System.out.println(m == n);

    结果:true。然后是int与int的比较,这就不说了,就是比较数值是否相等。最后是Integer与Integer的比较,这个稍微有点不同,

    (1) 两个通过new生成的变量:Integer变量实际上是对Integer对象的引用,所以,通过new产生的两个对象永远是不相等的。

            Integer i = new Integer(100);
            Integer j = new Integer(100);
            System.out.println(i == j);//结果为false

    (2)非new生成的和new生成的变量:非new生成的是自动装箱,调用的是Integer.valueOf(i),当值位于-128~127时,直接从cache中获取,这些cache引用对Integer对象地址是不变的,但是不在这个范围内的数字,则返回的是new Integer(i),是新建的地址 ,因此不相等。

            Integer i = new Integer(100);
            Integer j = 100;
            System.out.println(i == j);//结果为false

    (3)两个非new生成的变量:非new生成的是自动装箱,调用的是Integer.valueOf(i),当值位于-128~127时,返回true,不在这个范围内的数字返回false。

            Integer i = 100;
            Integer j = 100;
            System.out.println(i == j);//结果为true
            
            Integer m = 200;
            Integer n = 200;
            System.out.println(m == n);//结果为false

      具体怎么回事呢,就像我们说的自动装箱调用的是Integer.valueOf(i)方法,我们看看源码。

        public static Integer valueOf(int i) {
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }

      当 i >= IntegerCache.low && i <= IntegerCache.high时调用IntegerCache,而缓存的地址都是相同的,再看看IntegerCache类。

        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() {}
        }

      可以看到 IntegerCache.low = -128,如果你没有给JVM参数的话(一般都不会给),IntegerCache.high = 127。

     

      

    身体是革命的本钱,爱跑步,爱生活!
  • 相关阅读:
    【C++学习】C++中的new VS C语言中的malloc
    【C++学习】多态——解析树实例分析
    【C++错误处理】multiple definition of
    VS.NET控件命名规范
    ASP.NET跨页面传值技巧总结
    marquee+js实现某个区域的“无缝滚动效果”
    vs2008与oracle数据库连接
    C#中HashTable的用法
    1. asp.net实现单点登陆
    GridView实现某列中相同值合并(不规则表)
  • 原文地址:https://www.cnblogs.com/caozx/p/8552724.html
Copyright © 2011-2022 走看看