@Test public void testName() throws Exception { // 手動建立 Short s1 = new Short((short) 129); Short s2 = new Short((short) 129); System.out.println(s1 == s2); // false Short s3 = new Short((short) 126); Short s4 = new Short((short) 126); System.out.println(s3 == s4); // false /* * 結論: * 每次手動建立,都會建立一個新的對象。手動new的對象,地址是確定不會相同的。 * * 自動裝箱的對象,地址可能會相同,便可能會重用對象共享池中的對象。僅限經常使用少許的數據(範圍:-128<= x <= 127)。 * 數值較大的數據,在自動裝箱時,依然會建立一個新的對象。 */ // 自動裝箱 Short s11 = (short) 129; Short s22 = (short) 129; System.out.println(s11 == s22); // false Short s33 = (short) 126; Short s44 = (short) 126; System.out.println(s33 == s44); // true,享元模式,有個共享池,存放經常使用少許的數據(範圍:-128<= x <= 127) }