聊聊Java常量池

2018-08-12  本文已影响0人  方学顺

从老生常谈的包装类型“==” 和 “equals” 说起

最近在刷leetcode题时,又学到了很多之前没有注意到的点,随便写写,想到哪,写到哪吧。

    public static void main(String[] args) {
        //这里改成Integer integerA = new Integer(12)会对最终结果有影响,后面会说明
        Integer integerA = 12;
        Integer integerB = 12;
        System.out.println("integerA="+integerA+",integerB="+integerB);
        if(integerA == integerB){
            System.out.println("integerA == integerB");
        }else {
            System.out.println("integerA != integerB");
        }

        if(integerA.equals(integerB)){
            System.out.println("integerA equals integerB");
        }else {
            System.out.println("integerA not equals integerB");
        }

    }
integerA=12,integerB=12
integerA == integerB
integerA equals integerB
integerA=128,integerB=128
integerA != integerB
integerA equals integerB
    //Integer.java
    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }
    
        /**
     * Returns the value of this {@code Integer} as an
     * {@code int}.
     */
    public int intValue() {
        return value;
    }

何为常量池?

Integer.java
/**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} 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.
     *
     * This method will always cache values in the range -128 to 127,
     (划重点,内存中只缓存-128 to 127范围内的数据)
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
    
    
    Byte.java
    /**
     * Returns a {@code Byte} instance representing the specified
     * {@code byte} value.
     * If a new {@code Byte} instance is not required, this method
     * should generally be used in preference to the constructor
     * {@link #Byte(byte)}, as this method is likely to yield
     * significantly better space and time performance since
     * all byte values are cached.
     *(全部 byte 值已经缓存)
     *
     * @param  b a byte value.
     * @return a {@code Byte} instance representing {@code b}.
     * @since  1.5
     */
    public static Byte valueOf(byte b) {
        final int offset = 128;
        //这里加偏移量是因为byte对应的整数值和缓存索引值并非相等
        return ByteCache.cache[(int)b + offset];
    }
    
   
    /**
     *  Byte.java中的内部类
     */
        private static class ByteCache {
        private ByteCache(){}
        static final Byte cache[] = new Byte[-(-128) + 127 + 1];
        static {
            for(int i = 0; i < cache.length; i++)
                cache[i] = new Byte((byte)(i - 128));
        }
    }

知道了这些有什么用

op StringTable::basic_add(int index_arg, Handle string, jchar* name,
                           int len, unsigned int hashValue_arg, TRAPS) {

  // ...

  HashtableEntry<oop, mtSymbol>* entry = new_entry(hashValue, string()); // 新建
  add_entry(index, entry);    // 添加
  return string();
}
上一篇下一篇

猜你喜欢

热点阅读