package AdvanceJava;public class Box {public static void main(String args[]){Integer a = 127;Integer b = 127;System.out.println(a==b);Integer c = 128;Integer d = 128;System.out.println(c==d);/* * 如果 integer 封閉的對象 的大小 * 是在一個 位元組內的話,即 -128 - 127 * 就把封裝的對象放在常量池中, * 便如果封裝的對象大於一個位元組 的話 * 那麼就把其放在堆中 * 這了就是上面的 * a == b * c != d; * * 這了是一種享元模式 */}}package AdvanceJava;import static java.lang.System.*;public class Box { public static void main(String args[]) { Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Integer e = 321; Integer f = 321; Long g = 3L; out.println(c == d); out.println(e == f); out.println(c == (a+b)); out.println(c.equals(a+b)); out.println(g == (a+b)); out.println(g.equals(a+b)); out.println("--------"); /* * 如果 integer 封閉的對象 的大小 * 是在一個 位元組內的話,即 -128 - 127 * 就把封裝的對象放在常量池中, * 便如果封裝的對象大於一個位元組 的話 * 那麼就把其放在堆中 * 這了就是上面的 * a == b * c != d; * * 這了是一種享元模式 */ }}
package AdvanceJava;import static java.lang.System.*;public class Box { public static void main(String args[]) { Integer a = 1; Integer b = 2; Integer c = 3; Integer d = 3; Integer e = 321; Integer f = 321; Long g = 3L; out.println(c == d); out.println(e == f); out.println(c == (a+b)); out.println(c.equals(a+b)); out.println(g == (a+b)); out.println(g.equals(a+b)); out.println("--------"); /* * 如果 integer 封閉的對象 的大小 * 是在一個 位元組內的話,即 -128 - 127 * 就把封裝的對象放在常量池中, * 便如果封裝的對象大於一個位元組 的話 * 那麼就把其放在堆中 * 這了就是上面的 * a == b * c != d; * * 這了是一種享元模式 */ }}/* 反編譯後的代碼 * Integer a = Integer.valueOf(1); Integer b = Integer.valueOf(2);
Integer c = Integer.valueOf(3); Integer d = Integer.valueOf(3); Integer e = Integer.valueOf(321); Integer f = Integer.valueOf(321); Long g = Long.valueOf(3L); System.out.println(c == d); System.out.println(e == f); System.out.println(c.intValue() == a.intValue() + b.intValue()); System.out.println(c.equals(Integer.valueOf(a.intValue() + b.intValue()))); System.out.println(g.longValue() == a.intValue() + b.intValue()); System.out.println(g.equals(Integer.valueOf(a.intValue() + b.intValue()))); * */