zoukankan      html  css  js  c++  java
  • wrapper class (Integer 为例)

    1,导入 Integer a = 100; Integer b = 100; Integer c = 150; Integer d = 150; a == b; true c == d; false

    2,wrapper class(包装类) java是面向对象的语言,在java中,为了方便编程,设定了8中基本数据类型,分别是: byte,short,int,long,float,double,char,boolean 为了编程对象化操作,在java5之后,引入了包装类型,8中基本数据类型可以通过包箱,拆箱 机制和其对应的包装类进行相互的转换,基本数据类型对应的包装类分别是: Byte,Short,Integer,Long,Float,Double,Character,Boolean

    3,a == b , c == d,两个对象的比较,实际是内存地址的比较。

    Integer a = 100,等同于Integer a = Integer.valueOf(100);

        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 的时候,返回缓存里的 对象,当不满条件的时候,才会创建一个新的new Integer() 对象.

    4,解析,从缓存中取对象,还是创建新的对象的条件

    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 是Interger 的静态内部类。 IntegerCache.low : -128 IntegerCache.high : JRE可设置,不设置时候默认是127,通过属性java.lang.IntegerCache.high 来配置,从以上的代码可以看出,初始化的时候,会将统一创建值为-128 到 127 的对象放入 到Integer cache[] 中,所以赋值的时候,值若在cache的区间内,会之间从Integer.cache 中取出对象,而不用再次创建。

    5,包装类的low,high 值 Byte : -128 ~ 127 Short:-128 ~ 127 Integer:-128 ~ 127 Long:-128 ~ 127 Character :0 ~ 127

    6,设置包装类Integer.cache的high 值 一般来说,尽可能创建对象的时候,尽可能调用valueOf(),而不是 Integer i = new Integer(); 因为这样可以避免创建新的对象,减少内存开销,提高性能。 若想将缓存的容量扩大,(Integer:-128~127,length == 256),可通过修改VM args 来修改。

    具体方法如下 (Eclipse为例)

    点击Run,Run Configurations,找到Java Application,找到对应的Class,点击Arguments, 在VM Arguments 栏输入,-Djava.lang.Integer.IntegerCacheHigh = 1250,这样就将high 的值设为了1250了,也就扩大了缓存的容量。

  • 相关阅读:
    30 tcp编码, udp编码 pycharm输出带颜色, tcp实现的聊天室
    29 网络相关知识 socket
    c++ 程序
    solr 5.4安装
    Linux之ss
    filezilla 证书使用
    mongodb安装,使用
    wireshark抓包发现1506字节包
    python之模块的显要属性
    正则表达式之去除空行
  • 原文地址:https://www.cnblogs.com/pickKnow/p/9503931.html
Copyright © 2011-2022 走看看