在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ 今天看了一下integer源碼,為什么integer.valueOf(-120)

今天看了一下integer源碼,為什么integer.valueOf(-120)結(jié)果還是-120呢?

源碼如下:

    /**
     * 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,
     * 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);
    }
        int i=Integer.valueOf(-120);
        System.out.println(i);// i=-120
回答
編輯回答
遲月

IntegerCache.how是常量-128,IntegerCache.hight是常量127.

clipboard.png

2017年4月27日 17:00
編輯回答
苦妄

我理解你其實(shí)是想問,為什么i + (-IntegerCache.low)還返回i是么?
如果是的話,IntegerCache這個(gè)數(shù)組緩存了-128~127這些數(shù),分別放到了數(shù)組下標(biāo)為0~255的位置,所以i要+ (-IntegerCache.low),也就是將下標(biāo)+128。

2017年4月11日 16:16
編輯回答
離殤

調(diào)用Integer valueOf返回Integer對(duì)象,再調(diào)用Integer.intValue()方法得到value=-120.

2017年11月9日 06:51