zoukankan      html  css  js  c++  java
  • 用int还是用Integer?

    昨天例行code review时大家有讨论到int和Integer的比较和使用。 这里做个整理,发表一下个人的看法。
     
    【int和Integer的区别】
    • int是java提供的8种原始类型之一,java为每个原始类型提供了封装类,Integer是int的封装类。int默认值是0,而Integer默认值是null;
    • int和Integer(无论是否new)比较,都为true, 因为会把Integer自动拆箱为int再去比;
    • Integer是引用类型,用==比较两个对象,其实比较的是它们的内存地址,所以不同的Integer对象肯定是不同的;
    • 但是对于Integer i=*,java在编译时会将其解释成Integer i=Integer.valueOf(*);。但是,Integer类缓存了[-128,127]之间的整数, 所以对于Integer i1=127;与Integer i2=127; 来说,i1==i2,因为这二个对象指向同一个内存单元。 而Integer i1=128;与Integer i2=128; 来说,i1==i2为false。
    【各自的应用场景】
    • Integer默认值是null,可以区分未赋值和值为0的情况。比如未参加考试的学生和考试成绩为0的学生
    • 加减乘除和比较运算较多,用int
    • 容器里推荐用Integer。 对于PO实体类,如果db里int型字段允许null,则属性应定义为Integer。 当然,如果系统限定db里int字段不允许null值,则也可考虑将属性定义为int。
    • 对于应用程序里定义的枚举类型, 其值如果是整形,则最好定义为int,方便与相关的其他int值或Integer值的比较
    • Integer提供了一系列数据的成员和操作,如Integer.MAX_VALUE,Integer.valueOf(),Integer.compare(),compareTo(),不过一般用的比较少。建议,一般用int类型,这样一方面省去了拆装箱,另一方面也会规避数据比较时可能带来的bug。

    【附:Integer类的内部类IntegerCache和valueOf方法代码

    public final class Integer extends Number implements Comparable<Integer> {
    
    	//。。。。。。。。。。。。
    
        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) {
                    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);
                }
                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() {}
        }
    
        public static Integer valueOf(int i) {
            assert IntegerCache.high >= 127;
            if (i >= IntegerCache.low && i <= IntegerCache.high)
                return IntegerCache.cache[i + (-IntegerCache.low)];
            return new Integer(i);
        }
    	
    	//。。。。。。。。。。。。。
    
    }
    

    ref:java中申明变量用 int 还是 Integer http://bbs.csdn.net/topics/360102986

    ref:Integer与int的种种比较https://www.cnblogs.com/liuling/archive/2013/05/05/intAndInteger.html

  • 相关阅读:
    以色列人,印象
    周末之个人杂想(三)
    长春DotNet俱乐部会员群
    sharepoint中的ListViewWebPart和ViewToolBar的操作 Virus
    SharePoint2007中的WCM Virus
    SPWeb.ProcessBatchData Method Virus
    sharepoint中显示网页库item的webpart和显示列表库item的webpart Virus
    jquery在vs2008中智能提示的配置 Virus
    格式化sharepoint中取出来的字段值 Virus
    使用google的ajax API中的翻译小工具 Virus
  • 原文地址:https://www.cnblogs.com/buguge/p/8028502.html
Copyright © 2011-2022 走看看