1 Public voidTest1 () {2 intA = 128;3Integer B = 128;4Integer C = 128;5 //the integer is automatically disassembled into int, so the ture6System.out.println (a==b);7System.out.println (a==c);8 }9 Ten @Test One Public voidTest2 () { AInteger A = 127; -Integer B = 127; -System.out.println (A = = B);//The result here is true, there should be no doubt Java compiles to integer.valueof (127) When compiling the Integer C the -Integer C = 128; -Integer d = 128; -System.out.println (c = = d);//The result here is False, + /*The integer.valueof () method caches numbers between [-128,127] based on reducing the number of objects created and memory-saving considerations. - this number range incoming directly returns the object in the cache. Outside of this, direct new comes out. *///The source code is as follows + /*Public static Integer valueOf (int i) { A assert Integercache.high >= 127; at if (i >= integercache.low && i <= integercache.high) - return integercache.cache[i + (-integercache.low)]; - return new Integer (i); - }*/ -}
The similarities and differences between integers and ints in Java