[J2SE 1.5]Joshua Bloch咏Tiger诗八首

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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

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

各种特性的复杂程度

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》一章。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航