“Ease of Development”是J2SE 1.5的主要賣點。為此,這一版本加入了七種新的語言特性——Enhanced for Loop、Autoboxing/Auto-Unboxing、Varargs、Static Import、Enumeration、Metadata和Generics。使用這些特性,可以讓編寫出來的程序更簡練、更清晰、更不容易出錯,從而簡化開發的過程。
Joshua Bloch(《Effective Java》的作者,J2SE 1.5的主要開發人員之一)效仿William Blake(1757-1827,英國詩人)的《Tiger》一詩,為這些特性撰寫了這樣一組詩篇:
詠Tiger
Tiger, Tiger burning bright
Like a geek who works all night
What new-fangled bit or byte
Could ease the hacker"s weary plight?
這一首是總說Tiger中的能使開發變得簡單的各種新語言特性。
詠Enhanced for Loop
While Iterators have their uses
They sometimes strangle us like nooses
With enhanced-for"s deadly ray
Iterator"s kept at bay
這一首單表Enhanced for Loop機制提供了一種更簡單的遍歷數組、Collection和其它實現了java.lang.Iterable接口的對象的方法。
詠Autoboxing/Auto-Unboxing
When from the collections ints are drawn
Wrapper classes make us mourn
When Tiger comes, we"ll shed no tears
We"ll autobox them in the ears
這一首單表Autoboxing機制/Auto-Unboxing提供了一種更簡單的整合兩套不同的類型系統(基本類型和引用類型)的機制。
詠Varargs
O joyless nights, o joyless days
Our programs cluttered with arrays
With varargs here, we needn"t whine;
We"ll simply put the args inline
這一首單表Varargs機制提供了一種更簡單的傳遞個數可變的實參的方法。
詠Static Import
And from the constant interface
We shall inherit no disgrace
With static import at our side
Our joy will be unqualified
這一首單表Static Import機制提供了一種更簡單的訪問靜態成員的機制。
詠Enumeration
The int-enum will soon be gone
Like a foe we"ve known too long.
With type safe-enum"s mighty power
Our foe will bother us no more
這一首單表Enumeration機制提供了一種更簡單的定義“類型安全的枚舉類”的方法。
詠Metadata
As for noble metadata
I"ll have to sing its praises later
Its uses are so numerous
To give their due, I"d miss the bus
這一首單表Metadata機制提供了一種更簡單的定義“機器可讀的說明信息”的機制。
詠Generics
To the most despised collections" cast
We"ll bid a fond farewell at last
With generics" burning spear
The need for cast will disappear
這一首是說用Generics機制,可以免去很多強制類型轉化的步驟的事情。Generics機制還有些其它用法,在這里沒有提到。
posted on 2005-09-08 16:59
^ Mustang ^ 閱讀(1209)
評論(0) 編輯 收藏 所屬分類:
Java 點滴