java中Integet & int 区别 之浅度剖析

来源:互联网 发布:怎样下载办公室软件 编辑:程序博客网 时间:2024/06/04 00:21

今天看到了一个比较简单,但是面试时和容易出错的问题,那就是Integer 和 int 的之间的区别

int : 其实是java 的基本数据类型;
Java 有8大基本数据类型:boolean , char ,byte , short , int , long , float , double

Integer : Integer 是 int 的封装类,从Java5开始引入了自动装箱/拆箱机制,使得二者可以相互转换

代码示例:

public class test {     public static void main(String[] args) {         Integer  i1 =  new Integer(2);         Integer i2=2;         int i3=2;         System.out.println(i1==i2); // false   i1 是一个对象 ,  i1 和 i2 没有引用同一个对象         System.out.println(i1==i3); // true  i1 自动拆箱成了 int 类型 再和 i3 进行对比    }

下面看一个比较简单而易错的问题,正式因为它,才让我打算写这篇博客,虽然写的不好!

    public static void main(String[] args) {        Integer f1=100;        Integer f2=100;        Integer f3=120;        Integer f4=120;        Integer f5=130;        Integer f6=130;        String i1 ="123wswqdqddddqqqqqqqqqqqqqqqqqqqqqq";        String i2 ="123wswqdqddddqqqqqqqqqqqqqqqqqqqqqq";        System.out.println(i1==i2);  // true    //---------------------------------------------------           System.out.println(f1==f2);  // true         System.out.println(f3==f4);  // true         System.out.println(f5==f6);  // false     }

第一眼看到以上结果令人很诧异,这是为什么呢?

    其实 f1 , f2, f3, f4 , f5 ,f6    之间不是值的比较而是引用的比较 (因为Integer 是一个类), 100 , 120 , 130 是 int 类型 当int(基本数据类型) 类型 付给 Integer 封装类时 根据java 的自动装箱和拆箱机制 100 就会被自动装箱Integer(引用类型)

那么疑问又来了,我们在对比String 类型都的时候 就是true , 为什么Integer就是false呢?其实关键点就在于,当给一个Integer 赋值一个int 类型的时候 会调用 Integer 的 valueOf(int i) 静态方法

源码:

    /**     * Returns a <tt>Integer</tt> instance representing the specified     * <tt>int</tt> value.     * If a new <tt>Integer</tt> instance is not required, this method     * should generally be used in preference to the constructor     * {@link #Integer(int)}, as this method is likely to yield     * significantly better space and time performance by caching     * frequently requested values.     *     * @param  i an <code>int</code> value.     * @return a <tt>Integer</tt> instance representing <tt>i</tt>.     * @since  1.5     */     public static Integer valueOf(int i) {        if(i >= -128 && i <= IntegerCache.high)            return IntegerCache.cache[i + 128];        else            return new Integer(i);    }  

IntegerCache 是Integer 的内部类:源码

    private static class IntegerCache {        static final int high;        static final Integer cache[];        static {            final int low = -128;            // high value may be configured by property            int h = 127;            if (integerCacheHighPropValue != null) {                // Use Long.decode here to avoid invoking methods that                // require Integer's autoboxing cache to be initialized                int i = Long.decode(integerCacheHighPropValue).intValue();                i = Math.max(i, 127);                // Maximum array size is Integer.MAX_VALUE                h = Math.min(i, Integer.MAX_VALUE - -low);            }            high = h;            cache = new Integer[(high - low) + 1];            int j = low;            for(int k = 0; k < cache.length; k++)                cache[k] = new Integer(j++);        }        private IntegerCache() {}    } 

综合源码来说,当给Integer 赋值一个int 类型的时候(自动装箱),它会调用Integer 的 valueOf 静态方法 , 如果int 类型值在 -128 —— 127 之间 , 它不会创建新的Integer 对象 ,它会引用常量池中Integer 对象

注:其实 -128 —— 127 是一个字节 也就是 byte 的范围

原创粉丝点击