integer自動裝箱拆箱&String記憶體分配測試
阿新 • • 發佈:2019-02-01
int i1 = 1; int i2 = 1; System.out.println("i1==i2 : " + (i1 == i2)); // true Integer num1 = 1; int num2 = 1; System.out.println("num1 == num2 : " + (num1 == num2)); // true 自動拆箱 Integer test1 = 121; Integer test2 = 121; System.out.println("tet1 == test2 : " + (test1 == test2)); // true ( JVM會快取-128到127的Integer物件,不會產生新物件,直接指向快取地址) Integer obj1 = 1111; Integer obj2 = 1111; System.out.println("obj1 == obj2 : " + (obj1 == obj2)); // false 超過127之後就會產生新的物件 Integer one = new Integer(1); Integer aOne = new Integer(1); System.out.println("one == aOne : " + (one == aOne)); // false 屬於兩個不同物件 String am="test"; String amm="test"; System.out.println("am == amm :"+(am==amm));//true m 和mm指向同一地址 String wt=new String("tt"); String wtt=new String("tt"); System.out.println("wt == wtt :"+(wt==wtt));//false t 和tt 是兩個不同物件