Java 中裝箱拆箱的一個細節

來源:互聯網
上載者:User

 

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()))); * */

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.