快取常量池

2021-09-07 20:12:17 字數 622 閱讀 7673

亞信面試題:

先說結論

integer a=127;

integer b=127;

integer c=128;

integer d=128;

a==b true

c==d false

integer a=new integer (127);

integer b=new integer (127);

integer c=new integer (128);

integer d=new integer (128);

a==b false

c==d false

integer 內部有乙個-128到127的快取池,但是如果是new出來的,那每乙個物件都會去新建,不會用到快取池的資料

實測其他基本資料型別的包裝類都有這個快取池,包括:byte,short,long

public static integer valueof(int i)

/*** a cache of instances used by and auto-boxing

*/private static final integer small_values = new integer[256];

關於常量池

這裡暫且把integer作為包裝類的代表來說明.我們知道每次new都是建立乙個新的物件,由於 比較的是記憶體位址,所以下面的 不會為true string s1 new string 1 string s2 new string 1 system.out.println s1 s2 false但是下...

測試常量池

那天學些了常量池的一些特性,寫了一些 來驗證理論.1 public class testconstantspool 56 static void stringpool 1920 128 127以內的integer值 21static void integerpool 3132 33 128 127以...

常量池隨筆

常量池儲存了類,介面,方法等的常量資料,也儲存了string常量.如下 中a1,a2不是string常量 在編譯期即被確定 a3則是,a1.intern 也可以獲得string常量 是在執行時被裝載的常量 string a1 new string a a2 new string a string a...