[J2SE 1.5]Joshua Bloch咏Tiger诗八首

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?

这一首是总说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机制还有些其它用法,在这里没有提到。

相关内容

  • 逐渐挖掘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》一章。

你可能感兴趣的:(generics,J2SE,Collections,Arrays,import,border)