[J2SE 1.5]Joshua Bloch咏Tiger诗八首

类别:Java 点击:0 评论:0 推荐:
.article {padding:1em;background:white;} .abstract {padding:1em 1ex 1em 2em;} .author, .date {padding:0.5ex 0 0 1em;} h1.title, h2.major, h3.minor, h4.listing { margin:1ex 2px 1ex;color:#0066CC;font-family:sans-serif; } h1.title {margin-left:0;} h3.listing {margin:1ex 2px 0;} h4.label { margin:0 -1ex 1ex;padding:4px;border:1px solid #0066CC; color:white;background:#0066CC; } div.sidebar { margin:1.5em 0.5ex 1em;padding:0 1ex; border:2px solid #0066CC;-moz-border-radius:8px; } h4.label:first-child { -moz-border-radius-topleft:4px;-moz-border-radius-topright:4px;} .code { margin:0 0 1ex;padding:1em;background:#CCCCCC;font:monospace; border:1px solid #CCCCCC;-moz-border-radius:8px; } .subtitle {font-size:80%;padding-left:1ex;} dt {font-weight:bold;} li {margin-top:1ex;margin-bottom:1ex;} p {margin:1em 0;} .article a {text-decoration:underline;} .article a:link {color:#0000FF;} .article a:visited {color:#6699FF;} .article a:hover {color:#FF0000;background:#FFFF99;} Joshua Bloch咏Tiger诗八首

“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?


咏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


咏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


咏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


咏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


咏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



J2SE 1.5里新增的七个语言特性的复杂程度很不一致。大体说来,Enhanced for Loop、Autoboxing/Auto-Unboxing、Varargs和Static Import比较简单,而Enumeration、Metadata和Generics就比较复杂。


J2SE 1.5里并没有加入所有已经被发明的能简化开发的语言特性——例如给参数指派默认值的功能,就没有被包含。当然,如果只是想要做到这个效果的话,可以考虑用重载来代替。

相关内容 逐渐挖掘Enhanced for Loop 逐渐挖掘Autoboxing/Auto-Unboxing 逐渐挖掘Varargs 逐渐挖掘Static Import 参考资料 Joshua Bloch的诗作,取自Qusay H. Mahmoud的《The All-New Java 2 Platform, Standard Edition (J2SE) 1.5 Platform: Programming with the New Language Features in J2SE 1.5》一文。 关于“类型安全的枚举类”的更多信息,可以参考Joshua Bloch的《Effective Java》一书中的《Substitutes for C Constructs》一章。