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