Java编程思想(英文版·第4版)
作者 : Bruce Eckel
丛书名 : 经典原版书库
出版日期 : 2007-04-24
ISBN : 7-111-21250-8
定价 : 99.00元
教辅资源下载
扩展信息
语种 : 英文
页数 : 1482
开本 : 16开
原书名 : Thinking in Java (Fourth Edition)
原出版社: Prentice Hall
属性分类: 教材
包含CD :
绝版 :
图书简介

2003年《Software Development》杂志最佳书籍Jolt大奖
  2003年《Java Developer’s Journal》读者选择最佳书籍奖
  2001年《JavaWorld》编辑选择最佳书籍奖
  2000年《JavaWorld》读者选择最佳书籍奖
  1999年《Software Development》杂志最佳产品奖
  1998年《Java Developer’s Journal》编辑选择最佳书籍奖
  本书赢得了全球程序员的广泛赞誉,即使是最晦涩的概念,在Bruce Eckel的文字亲和力和小而直接的编程示例面前也会化解于无形。从Java的基础语法到最高级特性(深入的面向对象概念、多线程、自动项目构建、单元测试和调试等),本书都能逐步指导你轻松掌握。

  第4版特色
  ●适合初学者与专业人员的经典的面向对象叙述方式,为更新的Java SE5/6增加了新的示例和章节。
  ●测验框架显示程序输出。
  ●设计模式贯穿于众多示例中:适配器、桥接器、职责链、命令、装饰器、外观、工厂方法、享元、点名、数据传输对象、空对象、代理、单例、状态、策略、模板方法以及访问者。
  ●为数据传输引入了XML,为用户界面引入了SWT和Flash。
  ●重新撰写了有关并发的章节,有助于读者掌握线程的相关知识。
  ●专门为第4版以及Java SE5/6重写了700多个编译文件中的500多个程序。
  ●支持网站包含了所有源代码、带注解的解决方案指南、网络日志以及多媒体学习资料。
  ●覆盖了所有基础知识,同时论述了高级特性。
  ●详细地阐述了面向对象原理。
  ●在线可获得Java讲座CD,其中包含Bruce Eckel的全部多媒体讲座。
  ●在www.MindView.net网站上可以观看现场讲座、咨询和评论。

图书特色

图书前言

一开始,我只是将Java看作“又一种程序设计语言”。从许多方面看,它也的确如此。
但随着时间流逝,以及对Java的深入研究,我渐渐发现,与我所见过的其他编程语言相比,Java有着完全不同的核心目的。
程序设计其实是对复杂性的管理:待解决问题的复杂性,以及用来解决该问题的工具的复杂性。正是这种复杂性,导致多数程序设计项目失败。在我所知的所有程序设计语言中,几乎没有哪个将自己的设计目标专注于克服开发与维护程序的复杂性。当然,有些编程语言在设计决策时也曾考虑到复杂性的问题,然而,总是会有其他议题被认为更有必要加入到该语言中。于是不可避免地,正是这些所谓更必要的议题导致程序员最终“头撞南墙”。例如,C++选择向后兼容C(以便更容易吸引C程序员),以及具备C一样的高效率。这两点都是非常有益的设计目标,也确实促成了C++的成功,然而它们却暴露出更多的复杂性问题,而这也使得很多项目不得善终(你自然可以责怪程序员或者项目管理,但是,如果一种语言能够帮助你解决错误,那何乐而不为呢?)。再看一个例子,Visual Basic(VB)选择与Basic绑在一起,而Basic并未被设计为具备可扩展性的程序设计语言,结果呢,建立在VB之上的所有扩展都导致了无法维护的语法。还有Perl,它向后兼容awk、sed、grep,以及所有它打算替代的Unix工具,结果呢,人们开始指责Perl程序成了“不可阅读(write-only)的代码”(即,只要稍过一会儿,你就读不懂刚完成的程序了)。从另一个角度看,在设计C++、VB、Perl以及Smalltalk之类的程序设计语言时,设计师也都为解决复杂性问题做了某种程度的工作。并且,正是解决某类特定问题的能力,成就了它们的成功。
随着对Java的了解越来越深,Sun对Java的设计目标给我留下了最深刻印象,那就是:为程序员减少复杂性。用他们的话说就是:“我们关心的是,减少开发健壮代码所需的时间以及困难。”在早期,这个目标使得代码的运行并不快(Java程序的运行效率已经改善了),但它确实显著地缩短了代码的开发时间。与用C++开发相同的程序相比,采用Java只需一半甚至更少的开发时间。仅此一项,就已经能节约无法估量的时间与金钱了。然而Java并未止步于此。它开始着手解决日渐变得重要的各种复杂任务,例如多线程与网络编程,并将其作为语言特性或以工具库的形式纳入Java,这使得开发此类应用变得倍加简单。最终,Java解决了一些相当大的复杂性问题:跨平台编程、动态代码修改,甚至是安全的议题。它让你在面对其中任何一个问题时,都能从“举步维艰”到“起立鼓掌”。抛去我们都能看到的性能问题,Java确实非常精彩地履行了它的诺言:极大地提升程序员的生产率。
同时,Java正从各个方面提升人们相互通讯的带宽。它使得一切都变得更容易:编写程序,团队合作,创建与用户交户的用户界面,在不同类型的机器上运行程序,以及编写通过因特网通信的程序。
我认为,通讯变革的成果并不见得就是传输巨量的比特。我们所看到的真正变革是人与人之间的通讯变得更容易了:无论是一对一的通信,还是群体与群体之间,甚至整个星球之间的通信。我曾听闻,在足够多的人之间的相互联系之上,下一次变革将是一种全球意识的形成。Java说不定就是促进该变革的工具,至少,它所具备的可能性使我觉得,教授这门语言是非常有意义的一件事情。
Java SE5与SE6
本书的第4版得益于Java语言的升级。Sun起初称其为JDK1.5,稍后改作JDK5或J2SE5,最终Sun弃用了过时的“2”,将其改为Java SE5。Java SE5的许多变化都是为了改善程序员的体验。你将会看到,Java语言的设计者们并未完全成功地完成该任务,不过,总的来说,他们已经向正确的方向迈出了一大步。
新版的一个重要目标就是完整地吸收Java SE5/6的改进,并通过本书介绍以及应用这些变化。这意味着本书基本可以称之为“只限Java SE5/6”。并且,书中的多数代码并没有经过老版本的Java编译测试,所以如果你使用的是老版本的Java,编译可能会报错并中止。不过,我觉得这样利大于弊。
如果你不得不采用老版本的Java,我仍然为你在https://www. mindviewllc.com提供了本书早期版本的免费下载。基于某些原因,我决定不提供本书当前版本的免费电子版。
Java SE6
本书是一个非常耗时的,且具有里程碑意义的一个项目。就在本书出版之前,Java SE6(代号野马mustang)已经发布了beta版。虽然Java SE6中的一些小变化,对书中的代码示例有一点影响,但其主要的改进对本书的绝大部分内容并没有影响。因为Java SE6主要关注于提升速度,以及改进一些(不在本书讨论范围之内)类库的特性。
本书中代码全部用Java SE6的一个发布候选版(RC)进行过测试,因此我不认为Java SE6正式发布时会有什么变化能够影响本书的内容。如果到时真的有什么重要的改变,我将更新本书中的代码,你可以通过https://www.mindviewllc.com下载。
本书的封面已经指出,本书面向“Java SE5/6”。也就是说本书的撰写“面向Java SE5及其为Java语言引入的重大变化,同时也适用于Java SE6”。
第4版
为一本书写作新版时,作者最满意的是:把事情做得“恰如其分”。这是我从本书上一个版本发布以来所学到的东西。通常而言,这种见识正如谚语所云,“学习就是从失败中汲取教训。”并且,我也借机进行了一些修订。与往常一样,一个新的版本必将带来引人入胜的新思想。此时,新发现带来的喜悦,采用比以往更好的形式表达思想的能力,已经远远超过了可能引入的小错误。
这也是对不断在我脑中盘旋低语着的一种挑战,那就是让持有本书老版本的读者也愿意购买新的版本。这些促使着我尽可能改进,重写,以及重新组织内容,为热忱的读者们献上一本全新的,值得拥有的书。
改变
此版本中将不再包含以往本书中所携带的CD光盘。该CD中的重要部分《Thinking in C》的多媒体教程(由Chuck Allison为MindView创建),现在提供了可下载的Flash版本。该教程是为不熟悉C语法的读者所准备的。虽然,本书用了两章对语法做了较为完整的介绍,然而对于没有相应背景知识的读者而言,这也许仍然不够。而《Thinking in C》正是为了帮助这些读者提升到必要的程度。
完全重写了“并发”这一章(以前称为“多线程”),以符合Java SE5并发类库的重大改变。它将为读者了解并发的核心思想打下基础。如果没有这些核心的基础知识,读者很难理解关于线程的更复杂的议题。我花了很多个月撰写这一章,深陷“并发”的地狱之中,最终,这一章不仅涵盖了基础知识,而且大胆地引入了一些高级议题。
而对于Java SE5所具有的每一个重大的新特性,本书都有一个新的章节与之对应。其他的新特性则加入到了原有的章节中。我还一直在研究设计模式,因此在本书中,也介绍了设计模式的相关内容。
本书经历了重大的重组。这大多源自教授Java的过程,以及我对于“章节”的意义的重新思考。以前,我会不假思索地认为,每个“章节”应该包含一个“足够大的”主题。但是,在我教授设计模式的时候,我发现,如果每次只介绍一个模式(即使讲课的时间很短),然后立刻组织大家做练习,此时那些学员们的表现是最好的(我发现,这种节奏对于我这个老师而言也更有乐趣)。因此,在这一版中,我试着打破按主题划分章节的做法,也不理会章节的长度。我想,这也是一个改进。
我同样也认识到代码测试的重要性。必须要有一个内建的测试框架,并且每次你开发系统时都必须进行测试。否则,根本没有办法知道代码可靠与否。为了做到这一点,我开发了一个测试框架以显示和验证本书中每一个程序的输出结果。(该框架是用Python编写的,你可以在https://www.mindviewllc.com找到可下载的代码。)关于测试的话题在附录中有讨论。其中还包含了其他一些基本技术,我认为所有程序员都应该将它们加入到自己的工具箱中。
此外,我还仔细检查了书中的每一个示例,并且问我自己,“我为什么采用这种方式实现?”对大多数的示例,我都做了一定程度的修订与改进,使得这些示例更加贴切。同时,也传达出我所认为的Java编程中的最佳实践(至少起到抛砖引玉的作用)。许多以前的示例都经过了重新设计与重新编写,同时,删除了不再有意义的示例,也添加了新的示例。
读者们为此书的前三个版本提出了许多许多精彩的意见。这自然使我觉得非常高兴。不过,偶尔读者也会有抱怨,例如有读者埋怨“本书太长了”。对我而言,如果“页数太多”是你唯一的苦恼,那这真令人哭笑不得。(据说奥地利皇帝曾抱怨莫扎特的音乐“音符太多”!我可不是想把自己比作莫扎特。)此外,我只能猜测,发出这种抱怨的读者还不了解Java语言的博大精深,而且也没有看过这一领域的其他书籍。无论如何,在这一版中,我已经删减了过时无用,或不再重要的内容。总的来说,我已经尽我所能仔细复查了全书,进行了必要的增删与改进。对于删除旧的章节,我还是挺放心的。因为原始的材料在网站上都有(https://www. mindviewllc.com)。
对于仍然不能接受本书篇幅的读者,我向你们道歉。请相信,我已经尽我所能精简本书的长度了。
封面图片的故事
《Thinking in Java》的封面创作灵感来自于美国的 Arts & Crafts运动。该运动始于世纪之交,并在1900 到1920年间达到顶峰。它起源于英格兰,是对工业革命带来的机器产品和维多利亚时代高度装饰化风格的回应。Arts & Crafts强调简洁设计,而回归自然是其整个运动的核心,注重手工制造及推崇个性化设计,可是它并不回避使用现代工具。这和我们现今的情形有很多相似之处:世纪之交,从计算机革命的最初起源到对个人来说更精简、更意味深长的事物的演变,以及对软件开发技能而不仅是生产程序代码的强调。
我以同样的眼光看待Java:尝试将程序员从操作系统机制中解放出来,朝着“软件艺师”的方向发展。
我和封面设计者自孩提时代就是朋友,我们从这次运动中获得灵感,并且都拥有源自那个时期的(或受那个时期启发而创作的)家具、台灯和其他作品。
这个封面暗示的另一主题是一个收集盒,博物学家可以用它来展示他们保存的昆虫标本。这些昆虫可以看作是对象,并放置到“盒”这个对象当中,而盒对象又放置到“封面对象”当中,这形象地说明了面向对象程序设计中最为基本的“集合”概念。当然,程序员可能会不禁联想到“程序缺陷(bug)”;这些虫子被捕获,并假设在标本罐中被杀死,最后禁闭于一个展示盒中,似乎暗示Java有能力发现、显示和制服程序缺陷(事实上,这也是它最为强大的属性之一)。
在本版中,我创造了一幅水彩画,你可以在封面的背景中看到它。
致谢
首先感谢和我一起开研讨课、提供咨询和开发教学计划的这些合作者:Dave Bartlett、Bill Venners、Chuck Allison、Jeremy Meyer和 Jamie King。在我转而不停地竭力为那些像我们一样的独立人群开发在一起协同工作的最佳模式的时候,你们的耐心让我感激不已。
最近,无疑是因为有了Internet,我可以和极其众多的人一起合作,他们协助我一起努力,他们通常是在家办公。过去,我可能必须为这些人提供相当大的办公空间,不过由于现在有了网络、传真以及电话,我不需要额外的开销就可以从他们的帮助中受益。在我尽力学习更好地与其他人相处的过程中,你们都对我很有帮助,并且我希望继续学习怎样使我的工作能够通过借鉴他人的成果而变得更出色。Paula Steuer在接管我偶尔的商务活动时发挥了不可估量的价值,他使它们变得井井有条(Paula,感谢你在我懈怠时对我的鞭笞)。Jonathan Wilcox, Esq.详细审视了我公司的组织结构,推翻了每一块可能隐藏祸害的石头,并且使所有事情都条理化和合法化了,这让我们心服口服。感谢你的细心和耐心。Sharlynn Cobaugh使自己成为声音处理的专家,她是创建多媒体培训CD ROM和解决其他问题的精英成员之一。感谢你在面临难于处理的计算机问题时的坚定不移。在布拉格Amaio的人们也提出了一些方案来帮助我。Daniel Will-Harris最先受到在网上工作的启发,因此他当然是我所有设计方案的主要人物。
多年以来,Gernal Weinberg通过他的学术会议和研讨会,已经成为了我非正式的教练和导师,我十分感谢他。
Ervin Varga在第4版的技术纠正方面提供了巨大的帮助——尽管其他人在各个章节和示例方面也帮助良多,但是Ervin是本书最主要的技术复审者,他还承担了第4版的解决方案指南的重写任务。Ervin发现的错误和对本书所作的完善对本书来说价值连城。他对细节的投入和关注程度令人惊异,他是我所见过的远远超过其他人的最好的技术读者。感谢你,Ervin。
我在Bill Venners的www.Artima.com上的weblog,已经成为了当我需要交流思想时的一种解决之道。感谢那些通过提交评论帮助我澄清概念的人们,包括James Watson、Howard Lovatt、Michael Barker以及其他一些人,特别是那些在泛型方面提供帮助的人。
感谢Mark Welsh不懈的帮助。
Evan Cofsky一如既往地提供了有力的支持,他埋头处理了大量晦涩的细节,从而建立和维护了基于Linux的Web服务器,并保持MindView服务器始终处于协调和安全的状态。
一份特别的感谢要送给我的新朋友,咖啡,它为本项目产生了几乎无穷无尽的热情。当人们来到MindView研讨课时,科罗拉多州Crested Butte的Camp4 Coffee已经成为了标准住所,并且在研讨课中间休息期间,它是我所遇到的最好的饮食场所。感谢我的密友Al Smith,是他使这里成为如此好的一个地方,成为Crested Butte培训期间一个如此有趣和愉快的场所。还要感谢Camp4的所有泡吧常客们,很高兴他们总是为我们提供一些饮料。
感谢Prentice Hall的人们不断地为我提供我所需要的一切,并容忍我所有的特殊需求,而且不厌其烦地帮我把所有事情都搞定。
在我的开发过程中,有些工具已经被证明是无价的;但每次使用它们时都会非常感激它们的创建者。Cygwin (http://www.cygwin.com)为我解决了无数Windows不能解决的问题,并且每天我都会变得更加依赖它(如果在15年前当我的头脑因使用Gnu Emacs而搞得发懵的时候,能有这些该多好啊)。IBM的Eclipse (http://www.eclipse.org)对开发社区做出了真正杰出的贡献,并且随着它的不断升级,我期望能看到它的更伟大之处(IBM是怎样成为潮流所向的?我肯定错过了一份备忘录)。而JetBrains IntelliJ Idea则继续开阔着开发工具的创新之路。
我一开始就将Sparxsystems的Enterprise Architecture用于本书,并且它很快就成为了我选择的UML工具。Marco Hunsicker的Jalopy代码格式化器(www.triemax.com)在大量的场合都派上了用场,而且Marco在将其配置成满足我的特殊需求方面也提供了大量的帮助。我还发现Slava Pestov的JEdit及其插件经常会显得很有用(www.jedit.org),并且对于研讨课来说,它是非常适合初学者的编辑器。
当然,如果我在其他地方强调得还不够的话,我得再次重申,我经常使用Python(www.Python.org)解决问题,在我的密友Guido Van Rossum和PythonLabs那些身材臃肿愚笨的天才人物的智慧结晶的基础上,我花费了好几天的时间进行冲刺(Tim Peters,我现在已经把你借的鼠标加了个框,正式命名为TimBotMouse)。你们这伙人必须到更健康的地方去吃午餐。(还要感谢整个Python社区,他们是一帮令人吃惊的群体。)
很多人向我发送修正意见,我感激所有这些人,第1版特别要感谢:Kevin Raulerson(发现无数的程序缺陷),Bob Resendes(简直难以置信),John Pinto、Joe Dante、Joe Sharp(三位都难以置信),David Combs(校正了许多语法和声明),Dr. Robert Stephenson、John Cook、Franklin Chen、Zev Griner、David Karr、Leander A. Stroschein、Steve Clark、Charles A、Lee、Austin Maher、Dennis P. Roth、Roque Oliveira、Douglas Dunn、Dejan Ristic、Neil Galarneau、David B. Malkovsky、Steve Wilkinson以及许许多多的人。本书第1版在欧洲发行时,Marc Meurrens在电子版宣传和制作方面做出了巨大的努力。
感谢在本书第2版中使用Swing类库帮助我重新编写示例的人们,以及其他助手—Jon Shvarts、Thomas Kirsch、Rahim Adatia、Rajesh Jain、Ravi Manthena、Banu Rajamani、Jens Brandt、Nitin Shivaram、Malcolm Davis,还有所有表示支持的人。
在第4版中,Chris Grindstaff对SWT一节的撰写提供很多帮助,而Sean Neville为我撰写了Flex一节的第一稿。
每当我认为我已经理解了并发编程时,又会有新的奇山险峰等待我去征服。感谢Brian Goetz帮助我克服了在撰写新版本的“并发”一章时遇到的种种艰难险阻,并发现了其中所有的缺陷(我希望如此!)
对Delphi的理解使我更容易理解Java,这一点儿都不奇怪,因为它们有许多概念和语言设计决策是相通的。我的懂Delphi的朋友们给我提供了许多帮助,使我能够洞察一些非凡的编程环境。他们是Marco Cantu(另一个意大利人-难道会说拉丁语的人在学习Java时有得天独厚的优势?)、Neil Rubenking(直到发现喜欢计算机之前,他一直都在做瑜珈/素食/禅道),当然还有Zack Urlocker(最初的Delphi产品经理),他是我游历世界时的好伙伴。我们都很感激Anders Hejlsberg的卓越才华,他在C#领域不懈地奋斗着(正如你将在本书中看到的,C#是 Java SE5主要的灵感之一)。
我的朋友Richard Hale Shaw(以及Kim)的洞察力和支持都很有帮助。Richard和我花了数月时间将教学内容合并到一起,并为参加学习的学生设计出一套完美的学习体验。
书籍设计、封面设计以及封面照片是由我的朋友Daniel Will-Harris制作的。他是一位著名的作家和设计家(http://www.WillHarris.com),在计算机和桌面排版发明之前,他在初中的时候就常常摆弄刮擦信(rub-on letter),他总是抱怨我的代数含糊不清。然而,要声明的是,是我自己制作的照排好的(camera-ready)页面,所以所有排字错误都应该算到我这里。我是用Microsoft Word XP for Windows来编写这本书的,并使用Adobe Acrobat制作照排页面的。本书是直接从Acrobat PDF文件创建而来的。电子时代给我们带来了厚礼,我恰巧是在海外创作了本书第1版和第2版的最终稿—第1版是在南非的开普敦送出的,而第2版却是在布拉格寄出的。第3版和第4版则来自科罗拉多州的Crested Butte。正文字体是Georgia,而标题是Verdana。封面字体是ITC Rennie Machintosh。
特别感谢我的所有老师和我的所有学生(他们也是我的老师)。
Molly,在我从事这一版的写作时总是坐在我腿上,为我提供了她特有的温软而毛茸茸的支持。
曾向我提供过支持的朋友包括(当然还不止他们):Patty Gast(Masseuse extraordinary),Andrew Binstock,SteveSinofsky,JD Hildebrandt,Tom Keffer,Brian McElhinney,Brinkley Barr,《Midnight Engineering》杂志社的Bill Gates,Larry Constantine和Lucy Lockwood,Gene Wang,Dave Mayer,David Intersimone,Chris和Laura Strand,Almquists,Brad Jerbic,Marilyn Cvitanic,Mark Mabry,Dave Stoner,Cranstons,Larry Fogg,Mike Sequeira,Gary Entsminger,Kevin 和Sonda Donovan,Joe Lordi,Dave和Brenda Bartlett,Patti Gast,Blake,Annette&Jade, Rentschlers,Sudeks,Dick,Patty和Lee Eckel,Lynn和Todd以及他们的家人。当然还有我的父亲和母亲。

图书序言

Bruce Eckel 是MindView公司(www.MindView.net)的总裁,该公司向客户提供软件咨询和培训。他是C++标准委员会拥有表决权的成员之一,拥有应用物理学学士和计算机工程硕士学位。除本书外,他还是《C++编程思想》(该书影印版及翻译版已由机械工业出版社引进出版)的作者,并与人合著了《C++编程思想 第2卷》及其他著作。他已经发表了150多篇论文,还经常参加世界各地的研讨会并进行演讲。

作者简介

Bruce Eckel:Bruce Eckel:  Bruce Eckel 是MindView公司 (www.MindView.net) 的总裁,向客户提供软件咨询和培训。他是C++标准委员会拥有表决权的成员之一。他也是《Java编程思想》(该书第3版影印版及翻译版已由机械工业出版社引进出版)、《C++编程思想 第1卷》及其他C++著作的作者,已经发表了150多篇论文 (其中有许多C++语言方面的论文) ,他经常参加世界各地的研讨会并进行演讲。

图书目录

前言 ........................1
Java SE5与SE6........................3
Java SE6........................3
第4版........................4
改变........................4
封面图片的故事........................6
致谢........................7
Introduction........................13
Prerequisites ........................14
Learning Java....................... 14
Goals.................................... 15
Teaching from this book ...... 16
JDK HTML
documentation ..................... 17
Exercises............................... 17
Foundations for Java ...........18
Source code ..........................18
Coding standards ..........................21
Errors ...................................21
Introduction to Objects........................23
The progress
of abstraction ...................... 24
An object has
an interface.......................... 26
An object
provides services ................. 29
The hidden
implementation 30
Reusing the
implementation................... 32
Inheritance 33
Is-a vs.is-like-a relationships........................37
Interchangeable objects
with polymorphism............. 38
The singly rooted
hierarchy.............................. 43
Containers ........................... 44
Parameterized types (Generics)...45
Object creation & lifetime ... 46
Exception handling'
dealing with errors 49
Concurrent programming... 5o
Java and the Internet...........51
What is the Web?.......................... 51
Client-side programming 53
Server-side programming 59
Summary ............................. 6o
Everything Is an Object........................61
You manipulate objects
with references.....................61
You must create
all the objects....................... 63
Where storage lives ...................... 63
Special case: primitive types ........ 65
Arrays in Java ............................... 66
You never need to
destroy an obj eot ................. 67
Scoping ......................................... 67
Scope of objects ............................68
Creating new data types:
class .................................... 69
Fields and methods ...................... 70
Methods, arguments,
and return values ................ 72
The argument list ......................... 73
Building a Java program..... 74
Name visibility ............................. 74
Using other components...............75
The static keyword...................... 76
Your first Java program ...... 78
Compiling and running................ 80
Comments and embedded
documentation .....................81
Comment documentation ............ 82
Syntax........................................... 83
Embedded HTML ........................ 84
Some example tags....................... 85
Documentation example.............. 87
Coding style......................... 88
Summary............................. 89
Exercises.............................. 89
Operator........................93
Simpler print statements .... 93
Using Java operators .......... 94
Precedence .......................... 95
Assignment.......................... 95
Aliasing during method calls ....... 97
Mathematical operators...... 98
Unary minus
and plus operators ...................... l0l
Auto increment and
decrement.......................... l0l
Relational operators......... l03
Testing object equivalence........l03
Logical operators............... l05
Short-clrcmtmg..........................106
Literals............................... l08
Exponential notation ................. l09
Bitwise operators ................111
Shift operators.................... 112
Ternary if-else operator ... 116
String operator
+ and +-............................. 118
Common pitfalls
when using operators......... 119
Casting operators .............. 120
Truncation and rounding............121
Promotion................................... 122
Java has no "sizeof" 122
A compendium
of operators ....................... 123
Summary ............................133
Controlling Execution........................135
true and false ...................135
if-else................................. 135
Iteration.............................. 137
do-while 138
for............................................... 138
The comma operator .................. 14o
Foreach syntax .................. 14o
return............................... 143
break and continue ....... 144
The infamous "goto"......... 146
switch................................ 151
Summary ............................ 154
Initialization & Cleanup........................155
Guaranteed initialization
with the constructor........... 155
Method overloading .......... 158
Distinguishing
overloaded methods ................... 16o
Overloading with primitives........161
Overloading on return values..... 165
Default constructors.......... 166
The this keyword............... 167
Calling constructors
from constructors ....................... 170
The meaning of static 172
Cleanup: finalization
and garbage collection ....... 173
What is finalize() for? .............. 174
You must perform cleanup ......... 175
The termination condition.......... 176
How a garbage collector works ...178
Member initialization ........ 181
Specifying initialization ..............183
Constructor initialization... 185
Order of initialization .................185
static data initialization............ 186
Explicit static initialization ...... 190
Non-static
instance initialization ................. 191
Array initialization ............. 193
Variable argument lists.............. 198
Enumerated types ............. 204
Summary........................... 207
Access Control........................209
package:
the library unit .................. 210
Code organization .......................212
Creating unique
package names ............................ 213
A custom tool library 217
Using imports
to change behavior..................... 220
Package caveat ........................... 220
Java access specifiers.........221
Package access ............................221
public: interface access ............ 222
private: you can't touch that! ... 224
protected: inheritance access.. 225
Interface
and implementation.......... 228
Class access ....................... 229
Summary........................... 233
Reusing Classes........................237
Composition syntax 237
Inheritance syntax............. 241
Imtmhzmg the base class 244
Delegation ......................... 246
Combining composition
and inheritance ................. 249
Guaranteeing proper cleanup .... 251
Name hiding ............................... 255
Choosing composition
vs. inheritance ................... 256
protected......................... 258
Upcasting........................... 260
Why "upcasting"? ....................... 261
Composition vs. inheritance
revisited ...................................... 261
The final keyword ............ 262
final data.................................... 262
final methods ............................ 267
final classes ...............................270
final caution .............................. 271
Initialization
and class loading ............... 272
Initialization with inheritance.... 272
Summary ........................... 274
Polymorphism.......... 277
Upcasting revisited 278
Forgetting the object type .......... 279
The twist............................ 281
Method-call binding 281
Producing the right behavior .....282
Extensibility................................286
Pitfall: "ove rriding"
private methods....................... 290
Pitfall:fields
and static methods................... 290
Constructors and
polymorphism ................... 293
Order of constructor calls...........293
Inheritance and cleanup 295
Behavior of polymorphic
methods inside constructors...... 30l
Covariant return types...... 303
Designing
with inheritance ................ 304
Substitution vs. extension.......... 306
Downcasting and runtime
type information ........................ 308
Summary........................... 310
Interfaces..........311
Abstract classes
and methods....................... 311
Interfaces..........................316
Complete decouphng ........ 320
"Multiple inheritance"
in Java ............................... 326
Extending an interface
with inheritance ................ 329
Name collisions when
combining interfaces ................. 330
Adapting to an interface .... 331
Fields in interfaces ............ 335
Initializing fields in interfaces ... 335
Nesting interfaces ............. 336
Interfaces and factories..... 339
Summary........................... 343
Inner Classes.......... 345
Creating inner classes ....... 345
The link to
the outer .class ................... 347
Using .this and .new....... 350
Inner classes
and upcasting .................... 352
Inner classes in
methods and scopes .......... 354
Anonymous
inner classes ...................... 356
Factory Method revisited 361
Nested classes 364
Classes inside interfaces ............ 366
Reaching outward from
a multiply nested class ...............368
Why inner classes? ............ 369
Closures & callbacks ................... 372
Inner classes &
control frameworks .................... 375
Inheriting from
inner classes...................... 382
Can inner classes
be overridden?....383
Local inner classes ............ 385
Inner-class identifiers....... 387
Summary ...........................388
Holding Your Objects....389
Generics and
type-safe containers 390
Basic concepts ................... 394
Adding groups
of elements ........................ 396
Printing containers ........... 398
List.................................... 40l
lterator ............................406
Listlterator............................. 409
LinkedList....................... 4 l0
Stack................................. 412
Set ......................................415
Map................................... 419
Queue............................... 423
PriorityQueue ......................... 425
Collection vs. Iterator .. 427
Foreach and iterators ........ 431
The Adapter Method idiom........434
Summary ........................... 437
Error Handling
with Exceptions......443
Concepts ............................444
Basic exceptions ................ 445
Exception arguments..................446
Catching an exception....... 447
The try block.............................. 447
Exception handlers .................... 448
Creating your
own exceptions.................. 449
Exceptions and logging.............. 452
The exception
specification ...................... 457
Catching any exception..... 458
The stack trace ........................... 460
Rethrowing an exception............461
Exception chaining 464
Standard Java
exceptions.......................... 468
Special case:
RuntimeException ................ 469
Performing cleanup
with finally........................ 471
What's finally for? .................... 473
Using finally during return .... 476
Pitfall: the lost exception ............477
Exception restrictions ....... 479
Constructors...................... 483
Exception matching .......... 489
Alternative approaches ..... 490
History........................................ 492
Perspectives ............................... 494
Passing exceptions
to the console ............................. 497
Converting checked
to unchecked exceptions............ 497
Exception guidelines......... 500
Summary............................50l
Strings.......... 503
Immutable Strings .......... 503
Overloading '+' vs.
StringBuilder 504
Unintended recursion....... 509
Operations on Strings ...... 511
Formatting output..............514
printf() ...................................... 514
System.out.format() ............. 514
The Formatter class................. 515
Format specifiers........................ 516
Formatter conversions ............ 518
String.format() ....................... 521
Regular expressions .......... 523
Basics ..........................................524
Creating regular expressions...... 527
Quantifiers.................................. 529
Pattern and Matcher .............. 531
split() .........................................540
Replace operations ..................... 541
reset()........................................ 544
Regular expressions
and Java I/O ...............................544
Scanning input .................. 546
Scanner delimiters ...................549
Scanning with
regular expressions.....................550
StringTokenizer ............. 551
Summary ........................... 552
Type Information.......... 553
The need for RTI'I ............. 553
The Class object ............... 556
Class literals................................ 562
Generic class references ............. 565
New cast syntax ..........................568
Checking before a cast....... 569
Using class literals ...................... 576
A dynamic instanceof.............. 578
Counting recursively 580
Registered factories........... 582
instanceofvs. Class
equivalence........................ 586
Reflection: runtime
class information............... 588
A class method extractor ............590
Dynamic proxies 593
Null Objects....................... 598
Mock Objects & Stubs ................ 606
Interfaces and
type information ............... 607
Summary ............................613
Generics.......... 617
Comparison with C++....... 618
Simple generics..................619
A tuple library .............................621
A stack class ............................... 625
RandomList ............................ 626
Generic interfaces ............. 627
Generic methods ................631
Leveraging type
argument inference.................... 633
Varargs and generic methods .... 635
A generic-method
to use with Generators............ 636
A general-purpose Generator. 637
Simplifying tuple use ................. 639
A Set utility.................................641
Anonymous
inner classes 645
Building
complex models ................ 647
The mystery- of erasure ..... 650
The C+ + approach ..................... 652
Migration compatibility-............. 655
The problem with erasure.......... 656
The action at the boundaries ..... 658
Compensating
for erasure 662
Creating instances of types... 664
Arrays of generics................ 667
Bounds............................... 673
Wildcards ...........................677
How smart is the compiler?....... 680
Co ntravarian ce 682
Unbounded wildcards ............... 686
Capture conversion 692
Issues ................................. 694
No primitives
as type parameters .....................694
Implementing
parameterized interfaces ............ 696
Casting and warnings ................. 697
Overloading ................................699
Base class hijacks an interface .. 700
Self-bounded types............ 70l
Curiously-recurring generics...... 70l
Self-bounding .............................703
Argument covariance 706
Dynamic type safety ~ 710
Exceptions .......................... 711
Mixins ................................. 713
Mixins in C++ ............................. 714
Mixing with interfaces................ 715
Using the Decorator pattern....... 717
Mixins with dynamic proxies ..... 719
Latent typing 721
Compensating for
the lack of latent typing..... 726
Reflection.................................... 726
Applying a method
to a sequence .............................. 728
When you don't happen
to have the right interface .......... 731
Simulating latent typing
with adapters 733
Using function objects
as strategms ....................... 737
Summary: Is casting
really so bad?..................... 743
Further reading .......................... 746
Arrays.......... 747
Why arrays are special ...... 747
Arrays are
first-class object.......... 749
Returning an array............ 753
Multidimensional
arrays................................. 754
Arrays and generics........... 759
Creating test data .............. 762
Arrays.fill() ............................. 762
Data Generators...................... 763
Creating arrays
from Generators...................... 770
Arrays utilities.................. 775
Copying an array......................... 775
Comparing arrays ....................... 777
Array element comparisons....... 778
Sorting an array ......................... 782
Searching a sorted array ............ 784
Summary........................... 786
Containers in Depth.......... 791
Full container taxonomy.... 791
Filling containers .............. 793
A Generator solution............... 794
Map generators ......................... 796
Using Abstract classes .............800
Collection
functionality ...................... 809
Optional operations...........813
Unsupported operations............. 815
List functionality............... 817
Sets and storage order...... 821
SortedSet ................................. 825
Queues............................... 827
Priority queues........................... 828
Deques........................................ 829
Understanding Maps ........831
Performance 833
Sorted_Map............................... 837
LinkedHashMap 838
Hashing and hash codes ... 839
Understanding hashCode().....843
Hashing for speed....................... 847
Overriding hashCode()............ 851
Choosing
an implementation............ 858
A performance
test framework............................ 859
Choosing between Lists.............863
Microbenchmarking dangers ..... 871
Choosing between Sets .............. 872
Choosing between Maps............ 875
Utilities .............................. 879
Sorting and searching Lists...... 884
Making a Collection
or Map unmodifiable.................885
Synchronizing a
Collection or Map ...................887
Holding references............ 889
The WeakHashMap ................892
Java 1.o/1.1 containers...... 893
Vector & Enumeration......... 894
Hashtable.................................895
Stack.......................................... 895
BitSet......................................... 897
Summary ...........................900
I/O 901
The File class .................... 90l
A directory lister........................ 902
Directory utilities....................... 906
Checking for
and creating directories.............. 912
Input and output ............... 914
Types of InputStream ............. 915
Types of OutputStream .......... 917
Adding attributes
and useful interfaces ......... 918
Reading from an InputStream
with FilterlnputStream ......... 919
Writing to an OutputStream
with FilterOutputStream....... 921
Readers & Writers......... 922
Sources and sinks of data........... 923
Modifying stream behavior........ 924
Unchanged classes ..................... 925
Off by itself:
RandomAccessFile....... 926
Typical uses
of I/O streams................... 927
Buffered input file ...................... 927
Input from memory ................... 928
Formatted memory input .......... 929
Basic file output ......................... 930
Storing and recovering data....... 932
Reading and writing
random-access files.................... 934
Piped streams............................. 936
File reading
& writing utilities .............. 936
Reading binary files ................... 940
Standard I/O ......................941
Reading from standard input.....941
Changing System.out
to a PrintWriter ...................... 942
Redirecting standard I/O........... 942
Process control.................. 944
New I/O............................. 946
Converting data 950
Fetching primitives .................... 953
View buffers ............................... 955
Data manipulation
with buffers ................................ 96o
Buffer details.............................. 962
Memory-mapped files 966
File locking ................................. 970
Compression ..................... 973
Simple compression
with GZIP ................................... 974
Multifile storage with Zip........... 975
J ava ARchives (JARs) ................ 978
Object serialization ........... 980
Finding the class........................ 984
Controlling serialization............ 986
Using persistence .......................996
XML................................. l003
Preferences ...................... l006
Summary ......................... l008
Enumerated Types.......... 1011
Basic enum features ....... 1011
Using static imports
with enums............................. l013
Adding methods
to an enum ...................... l014
Overriding enum methods..... l015
enums in
switch statements........... 1016
The mystery
of values()....................... 1017
Implements,
not inherits..................... l020
Random selection............. 1021
Using interfaces
for organization............... 1022
Using EnumSet
instead of flags................ l028
Using EnumMap........... l030
Constant-specific
methods ........................... l032
Chain of Responsibility
with enums............................. l036
State machines with enums... l041
Multiple dispatching ....... l047
Dispatching with enums ......... l050
Using
constant-specific methods....... l053
Dispatching
with EnumMaps..................... l055
Using a 2-D array 1056
Summary.......................... l057
Annotations......................... l059
Basic syntax l060
Defining annotations ................ l061
Meta-annotations..................... l063
Writing
annotation processors.... l064
Annotation elements............... l065
Default value constraints ......... l065
Generating external files......... l066
Annotations don't
support inheritance................. l070
Implementing the processor.... l071
Using apt to
process annotations .........l074
Using the Visitor pattern
with apt........................... l079
Annotation-based
unit testing l083
Using @Unit with generics.... l094
No "suites" necessary l095
Implementing @Unit.............. l096
Removing test code................... 1104
Summary.......................... 1106
Concurrency............ 1109
The many faces of
concurrency....................... 1111
Faster execution......................... 1111
Improving code design ..............1114
Basic threading ................ 1116
Defining tasks ............................1116
The Thread class ..................... 1118
Using Executors...................... 1120
Producing return values
from tasks.................................. 1124
Sleeping..................................... 1126
Priority ...................................... 1127
Yielding ......................................1129
Daemon threads ....................... 113o
Coding variations ...................... 1135
Terminology ............................1142
Joining a thread...... .............. 1143
Creating responsive
user interfaces ........................... 1145
Thread groups ...........................1146
Catching exceptions................... 1147
Sharing resources............. 1150
Improperly
accessing resources .................. 1150
Resolving shared
resource contention................... 1153
Atomicity and volatility............ 1160
Atomic classes ...........................1167
Critical sections .........................1169
Synchronizing on
other objects .............................. 1175
Thread local storage .................. 1177
Terminating tasks ............ 1179
The ornamental garden ............. 1179
Terminating when blocked....... 1183
I nterruptio n ............................... 1185
Checking for an interrupt..........1194
Cooperation
between tasks ................... 1197
wait() and notifyAll() 1198
notify() vs. notifyAll().......... 1204
Producers and consumers ........ 1208
Producer-consume rs
and queues................................. 1215
Using pipes for I/O
between tasks ............................1221
Deadlock.......................... 1223
New library
components ..................... 1229
CountDownLatch................. 1230
CyclicBarrier......................... 1232
DelayQueue............................ 1235
PriorityBlockingQueue ....... 1239
The greenhouse controller
with ScheduledExecutor...... 1242
Semaphore .............................1246
Exchanger.............................. 1250
Simulation ........................1253
Bank teller simulation............... 1253
The restaurant simulation........1259
Distributing work......................1264
Performance tuning .........1270
Comparing
mutex technologies 1271
Lock-free containers 1281
Optimistic locking ........................ 1290
ReadWrite Locks................... 1292
Active objects ................... 1295
Summary......................... 1300
Further reading........................ 1302
Graphical User Interfaces........................ 1303
Applets............................. 1306
Swing basics .....................1307
A display framework ................. 1310
Making a button................1311
Capturing an event........... 1312
Text areas ......................... 1315
Controlling layout ............ 1317
BorderLayout ........................ 1317
FlowLayout ............................ 1318
GridLayout ............................. 1319
GridBagLayout..................... 1320
Absolute positioning ................ 1320
BoxLayout ............................. 1320
The best approach? ................... 1321
The Swing event model.... 1321
Event and listener types............1322
Tracking multiple events ..........1329
A selection of
Swing components .......... 1332
Buttons ..................................... 1333
Icons ........................................ 1335
Tool tips .................................... 1337
Text fields ................................. 1338
Borders .....................................1340
A mini- editor..............................1341
Check boxes .............................. 1342
Radio buttons ........................... 1344
Combo boxes
(drop-down lists).............................1345
List boxes..............................1347
Tabbed panes............................ 1349
Message boxes .......................... 1350
Menus ....................................... 1352
Pop-up menus 1359
Drawing .................................... 1360
Dialog boxes ............................. 1364
File dialogs................................ 1368
HTML on
Swing components ................... 1370
Sliders and progress bars .......... 1371
Selecting look & feel ................. 1373
Trees, tables & clipboard.......... 1376
JNLP and
Java Web Start .................1376
Concurrency & Swing...... 1382
Long-running tasks ..................1382
Visual threading ........................1391
Visual programming
and JavaBeans................. 1393
What is a JavaBean?................. 1395
Extracting BeanInfo
with the Introspector............ 1397
A more sophisticated Bean........... 1403
JavaBeans
and synchronization ................. 1407
Packaging a Bean.......................1412
More complex Bean support..... 1414
More to Beans ........................... 1415
Alternatives to Swing ....... 1415
Building Flash Web
clients with Flex............... 1416
Hello, Flex ................................. 1416
Compiling MXML ..................... 1418
MXML and ActionScript........... 1419
Containers and controls........... 142o
Effects and styles.......................1422
Events ........................................1423
Connecting to Java....................1424
Data models
and data binding ....................... 1427
Building and deploying............ 1428
Creating SWT
applications ..................... 1430
Installing SWT .......................... 1431
Hello, SWT................................ 1431
Eliminating redundant code.....1434
Menus........................................1436
Tabbed panes, buttons,
and events ................................ 1438
Graphics ....................................1442
Concurrency in SWT.................1444
SWT vs Swing? 1447
Summary .......................... 144.7
Resources ................................. 1448
A: Supplements................................ 1449
Downloadable supplements .................... 1449
Thinking in C:
Foundations for lava ...... 1449
Thinking in lava
seminar............................ 1450
Hands-On Java
seminar-on-CD.................. 1450
Thinking in Objects
seminar............................ 1450
Thinking in
Enterprise ,lava ................ 1451
Thinking in Patterns
(with Java)....................... 1452
Thinking in Patterns
seminar............................ 1452
Design consulting
and reviews.......................1453
B: Resources................. 1455
Software............................ 1455
Editors & IDEs .................1455
Books ............................... 1456
Analysis & design...................... 1457
Python....................................... 1460
My own list of books................. 1460

教学资源推荐
作者: 郭志强 邱李华 曹青 等编著
作者: [美] 约翰·戴维·斯通(John David Stone) 著
作者: 宋存利 张雪松 编著
作者: 陈作聪 苏静 王龙 等编著
参考读物推荐
作者: 许进 叶志远 钟尊发 蔡波斯 等著
作者: 陶国荣 著
作者: Cay S. Horstmann, Gary Cornell
作者: [美] 道格·赫尔曼(Doug Hellmann)著