-128----127缓存,在栈中获取

</pre><span style="font-size: 13.3333px;">package test;</span><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">import java.util.HashMap;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">import java.util.HashSet;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">import java.util.Map;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">import java.util.Set;</span><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">import org.apache.commons.logging.Log;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">import org.apache.commons.logging.LogFactory;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">import org.slf4j.Logger;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">import org.slf4j.LoggerFactory;</span><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">public class Test {</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">/*</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;"> * 看程序写结果</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;"> * </span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;"> * 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;"> */</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">    public static void main(String[] args) {</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer i1 = new Integer(127);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer i2 = new Integer(127);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println(i1 == i2);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println(i1.equals(i2));</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println("-----------");</span><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer i3 = new Integer(128);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer i4 = new Integer(128);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println(i3 == i4);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println(i3.equals(i4));</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println("-----------");</span><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer i5 = 128;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer i6 = 128;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println(i5 == i6);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println(i5.equals(i6));</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println("-----------");</span><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer i7 = 127;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer i8 = 127;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println(i7 == i8);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        System.out.println(i7.equals(i8));</span><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        // 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">        Integer ii = Integer.valueOf(127);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px; white-space: pre;">	</span><span style="font-size: 13.3333px;">    }</span><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><br style="font-size: 13.3333px;" /><p><span style="font-size: 13.3333px;">}</span></p><p><span style="font-size: 13.3333px;">public static Integer valueOf(int i) {</span></p><span style="font-size: 13.3333px;">        assert IntegerCache.high >= 127;</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">        if (i >= IntegerCache.low && i <= IntegerCache.high)</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">            return IntegerCache.cache[i + (-IntegerCache.low)];</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">        return new Integer(i);</span><br style="font-size: 13.3333px;" /><span style="font-size: 13.3333px;">    }</span><pre>
package test;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




public class Test {

/*
 * 看程序写结果
 * 
 * 注意:Integer的数据直接赋值,如果在-128到127之间,会直接从缓冲池里获取数据
 */
    public static void main(String[] args) {
        Integer i1 = new Integer(127);
        Integer i2 = new Integer(127);
        System.out.println(i1 == i2);
        System.out.println(i1.equals(i2));
        System.out.println("-----------");


        Integer i3 = new Integer(128);
        Integer i4 = new Integer(128);
        System.out.println(i3 == i4);
        System.out.println(i3.equals(i4));
        System.out.println("-----------");


        Integer i5 = 128;
        Integer i6 = 128;
        System.out.println(i5 == i6);
        System.out.println(i5.equals(i6));
        System.out.println("-----------");


        Integer i7 = 127;
        Integer i8 = 127;
        System.out.println(i7 == i8);
        System.out.println(i7.equals(i8));


        // 通过查看源码,我们就知道了,针对-128到127之间的数据,做了一个数据缓冲池,如果数据是该范围内的,每次并不创建新的空间
        Integer ii = Integer.valueOf(127);
    }


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值