java 基本数据类型相关思考

1:自动装箱、拆箱

例子:

Integer a = 10; //this is autoboxing
//上面的操作其实就是下面这个,编译器会帮你自动转,不用手动
Integer b = Integer.valueOf(10); //under the hood

看下Integer中valueOf(int i)的方法介绍

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);
    }

好像是IntegerCache中去查找 i,找到了就返回,找不到就自己new一个Integer(i);

那我们去看看IntegerCache是个什么东西...

2:Integer的缓存机制

 其实IntegerCache是Integer的一个内部类

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

  Javadoc:详细的说明这个类是用来实现缓存支持,并支持 -128 到 127 之间的自动装箱过程。最大值 127 可以通过 JVM 的启动参数 -XX:AutoBoxCacheMax=size 修改。 缓存通过一个 for 循环实现。从小到大的创建尽可能多的整数并存储在一个名为 cache 的整数数组中。这个缓存会在 Integer 类第一次被使用的时候被初始化出来。以后,就可以使用缓存中包含的实例对象,而不是创建一个新的实例(在自动装箱的情况下)。

 这种缓存行为不仅适用于Integer对象。我们针对所有整数类型的类都有类似的缓存机制。
  有 ByteCache 用于缓存 Byte 对象
  有 ShortCache 用于缓存 Short 对象
  有 LongCache 用于缓存 Long 对象
  有 CharacterCache 用于缓存 Character 对象
 
 Byte,Short,Long 有固定范围: -128 到 127。对于 Character, 范围是 0 到 127。除了 Integer 可以通过参数改变范围外,其它的都不行。

IntegerCache中的定义的Integer cache[]数组中存的就是Integer类型的缓存对象,谈到这里,我们不禁想问,为什么缓存Integer而不缓存int这种基本数据类型?

 

其实原因有好多种,可以大致说出几个原因:

1)Integer是对象类型,存在堆上面,int这个数据类型时基本数据类型,存在栈上面 or 堆上面?,栈上面是临时的,容易出栈;

  基本数据类型是放在栈中还是放在堆中,这取决于基本类型声明的位置。

3:基本数据类型存放位置

 一:在方法中声明的变量,即该变量是局部变量,每当程序调用方法时,系统都会为该方法建立一个方法栈,其所在方法中声明的变量就放在方法栈中,当方法结束系统会释放方法栈,其对应在该方法中声明的变量随着栈的销毁而结束,这就局部变量只能在方法中有效的原因

      在方法中声明的变量可以是基本类型的变量,也可以是引用类型的变量。

         (1)当声明是基本类型的变量的时,其变量名及值(变量名及值是两个概念)是放在方法栈中

         (2)当声明的是引用变量时,所声明的变量(该变量实际上是在方法中存储的是内存地址值)是放在方法的栈中,该变量所指向的对象是放在堆类存中的。

   二:在类中声明的变量是成员变量,也叫全局变量,放在堆中的(因为全局变量不会随着某个方法执行结束而销毁)。

       同样在类中声明的变量即可是基本类型的变量 也可是引用类型的变量

       (1)当声明的是基本类型的变量其变量名及其值放在堆内存中的

       (2)引用类型时,其声明的变量仍然会存储一个内存地址值,该内存地址值指向所引用的对象。引用变量名和对应的对象仍然存储在相应的堆中

  所以如果Integer cache[]数组中存的int这个基本数据类型,有可能在栈上面就会丢失,达不到cache的效果

2)Integer是对象类型,对象类型持久,更加符合面向对象的思想?

原文地址:https://www.cnblogs.com/myseries/p/12974572.html