Java 5.0的特性

來源:互聯網
上載者:User

自動裝箱/拆箱 (Auto-Boxing/Unboxing)

沒有自動裝箱/拆箱:

int int1 = 1;
Integer integer2 = new Integer(int1);
int int3 = integer2.intValue();

有自動裝箱/拆箱:

int int1 = 1;
Integer integer2 = int1;   // 自動裝箱
int int3 = integer2;     // 自動拆箱

泛型 (Generic Types)

泛型就像是C++的模板。原有的Collection API加上泛型支援後,增加對型別的檢查,減 少程式錯誤的機會。

沒有泛型:

HashMap hm = new HashMap();
int i=1;
String tt="test";
hm.put(new Integer(i), tt);

使用Generic:

HashMap <Integer, String>hm = new HashMap<Integer, String> ();
int i=1;
String tt = "test";
hm.put(i, tt);   // 在這裏對int自動裝箱成Integer,也使用了參數的型別檢查

自動裝箱的新功能,可能是從C#語言身上學習來的,Java 已經越來越像C#。然而Java對 自動裝箱/拆箱的支援,僅是利用編譯器實現,在Java Bytecode 中,並無自動裝箱/拆箱的 作業碼 (opcode)。

注釋 (Annotation)

Annotation全名是Program Annotation Facility ,是Java SE 5.0的新功能。Java 的 Annotation 類似於 .NET 的屬性 (Attribute)。Java 的注釋是一種介面 (interface),繼 承自 java.lang.annotation.Annotation。Class File 則貼上 ACC_ANNOTATION 標籤。

// JDK 1.4
/**
  * @todo to be implemented
  **/
void gimmeSomeLoving() {
  throw new Exception("not implemented");
}

// JDK 1.5
@todo void gimmeSomeLoving() {
  throw new Exception("not implemented");
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.