评注版 Java编程思想第4版


评注版 Java编程思想第4版

文章插图
Java编程思想第4版(评注版)【评注版 Java编程思想第4版】《Java编程思想第4版(评注版)》是2011年电子工业出版社出版的图书 。作者是埃克尔 , 刘中兵评注 。《Java编程思想(评注版第4版)》是“传世经典书丛”之一 , 作者拥有多年教学经验 , 对C、C++以及Java语言都有独到、深入的见解 , 书中以通俗易懂且小而直接的示例阐释了一个个晦涩抽象的概念 , 是一本当之无愧的经典之作 , 书中包括基本语法、面向对象、高级特性等五部分内容 。本书适合从事相关研究工作的人员参考阅读 。
基本介绍书名:Java编程思想第4版(评注版)
作者:(美)埃克尔(Eckel, B.)、刘中兵
ISBN:978-7-121-13521-7
页数:756页
定价:108.00元
出版社:电子工业出版社
出版时间:2011年6月
开本:16开
内 容 简 介本书作者拥有多年教学经验 , 对C、C++以及Java语言都有独到、深入的见解 , 书中以通俗易懂且小而直接的示例阐释了一个个晦涩抽象的概念 , 是一本当之无愧的经典之作 。本评注版讲解了Java设计、语法和库的各个方面 , 包括Java的运算符、控制逻辑、构造、回收、重用、接口、内部类、存储、异常、字元串、类型、泛型、数组、容器、I/O、注释、并发等内容 。对于国外技术图书 , 选择翻译版还是影印版 , 常常让人陷入两难的境地 。本评注版力邀国内资深专家执笔 , 在英文原着基础上增加中文点评与注释 , 旨在融合二者之长 , 既保留经典的原创文字与味道 , 又以先行者的学研心得与实践感悟 , 对读者阅读与学习加以点拨、指明捷径 。经过评注的版本 , 更值得反覆阅读与体会 。希望这本书能够帮助您跨越Java的重重险阻 , 领略高处才有的壮美风光 , 做一个成功而快乐的Java程式设计师 。评注者序与Thinking in Java的渊源学习Java的道路好比西天取经 , Java就是要取得的真经 , 我们都是去往Java之路的圣徒 。学习Java不仅要经历九九八十一难 , 更要理解Java这本圣经的精髓 。大家都觉得Java之路不好走 , 因为它不仅仅是Java , 还有无数的技术 , 如JSP、Servlet、JavaBean、J2EE、Struts、Spring、Hibernate、资料库SQL、SOA分散式、快取、JVM、Eclipse、Tomcat、HTTP、JSON等等 , 它们组成了取经道路上的九九八十一难 。众多技术 , 围绕的精髓是Java 。在学习Java的道路上 , 我也经历过九九八十一难 。我通过编写《Java高手真经》对Java道路上的层层技术做了一个自认为完整的总结 , 也是对自己步入Java世界以来的种种所见所闻所感的记录和升华 。但此时 , 对Java我有种被掏空的感觉 。因为 , 我觉得自己对Java的精髓理解得还是不够 。偶然的机会接到博文视点的邀约 , 为这本《Java编程思想》(Thinking in Java)的英文版本做点评 。儘管我写过很多技术类的书 , 但对于这种圣经式的经典着作 , 借用时下最流行的一句话 , 内心有一点“忐忑”? 。因为 , 对经典着作做的点评 , 必须是点睛之笔 , 对原文要能够画龙点睛 。想到这里 , 内心就更加忐忑了!为此 , 我抱着学习交流的心态重读了这本书 。刚学习Java时 , 往往会经资深人士推荐说:“读这本《Java编程思想》才是学真正的Java , 国人所着太浅!”于是怀着崇拜之情 , 读之 , 傻眼 , 1000多页、大量本质的分析 , 再加上蹩脚的英语 , 读之皮毛矣!从此也就束之高阁 。何时应该读这本书李彦宏关于网际网路的发展曾说过:“时机很重要 , 进去太早会饿死 , 太晚就没有机会了 。”实际上这句话在这里也适用 , 太早读这本书会难以理解 , 太晚你会后悔为什幺才看到这本书 。这是因为 , 正如书名所言 , 它讲解的是Java编程的内在思想 , 只有拥有了一定Java编程基础和开发经验后 , 读它才是对Java理解的升华!因此 , 这本书最大的作用不是让你“知其然” , 而是让你“知其所以然”!如何读这本书本书原英文版涵盖了Java设计、语法和库的各个方面 , 包括Java的运算符、控制逻辑、构造、回收、重用、多态、接口、内部类、存储、异常、字元串、类型、泛型、数组、容器、I/O、注释、并发、图形GUI 。原英文版是按美国人的习惯散列排列的 , 本书为了更加一目了然 , 更加符合国人的思维方式 , 将这些章节分为五个大的部分 , 每一部分都涵盖了深入分析的独家内容 。第1部分 基本语法:运算符、控制逻辑运算符和控制逻辑是任何一门语言的最基础语法 , Java参考了C++的优点 , 扬长避短 , 因此它的运算符也基于C++ , 但在某些方面进行了简化和改进 。熟悉C++将会对本部分轻车熟路 。Java中的控制语句共提供了如下几个关键字:if/else/while/do-while/for/return/break/continue/switch 。虽然有goto , 但是不要使用 , 它会使代码混乱 。在Java中已经摒弃了goto语句 。第2部分 面向对象:对象的创建与销毁、访问控制、重用、接口、内部类、异常本书是关于类的初始化和销毁讲解最为完美的一本书 , 细緻、全面、易懂 。访问控制专门开闢了一章 , 对于类和包的访问控制关係讲解小巧到位 。要让每个人编写的程式能够为他人所用 , 首先是类的包结构清晰 , 其次是代码中的函式包装清晰 。第3部分 数据存储:字元串、数组、存储、容器java.util包中提供了纷繁複杂的容器类 , 包括集合类Collection(包括Queue先进先出伫列、List允许重複的有序列表、Set不允许重複的集合)和映射类Map(键值对) 。本书深度分析了基于这些接口的各种实现类的使用、工作原理 。“Containers in Depth”一章是讲解Java容器类最深入的部分 , 它会告诉你何时应该使用哪些容器、哪些是最有用的、为什幺使用以及怎幺使用 。第4部分 核心功能:输入输出、并发I/O提供了Java与外部系统进行通信的基础库 , 包括控制台、档案、网路 。这些是I/O需要涉及的方方面面 。输入输出的方式包括顺序读取、随机读取、快取、二进制、字元、行读取、位元组读取等等 , 因此I/O也提供了各种读写的包装类 。本书通过大量实例来演示这些类的使用方法和套用场景 。多执行绪是与单执行绪比较而言的 , 本书从基本的Thread、Runnable的使用讲起 , 深入且全面讲解了执行绪的休眠、优先权、Daemon、执行绪的异常处理、资源共享、执行绪的控制、与其他执行绪的协作、死锁等 , 并讲解了Java中使用的一些执行绪工具类 。第5部分 高级特性:类型、泛型、枚举、注释书中从Class类、cast、静态分析、动态分析、动态代理、Mock等各方面讲解类的RTTI解析 , 使用Generic实现对Java的扩展 。JDK 5.0通过名为注释(Annotation)的新功能 , 将一个更通用的元数据工具合併到核心Java语言中 。对于开发者来说 , 不仅可以使用JDK内置的注释 , 还可以使用第三方提供的强大注释功能 , 比如单元测试、EJB、Hibernate等 , 还可以自定义注释 。我的建议由于AWT和Swing目前在实际套用中比例不高 , 并且擅长界面编程的中文图书比比皆是 , 因此就删除了图形化编程GUI一章 。另外 , 本书开头的两章 , 即“Introduction to Objects”和“Everything Is an Object”没有收入本书 。还请读者留意以下两点:(1)由于本评注版经重新编排 , 章节顺序与原着不尽相同 , 加之部分章节有所节略 , 因此书中涉及引用章节之处与图书现状略有出入 。由此给读者带来的不便 , 还请谅解 。(2)各章练习序号后的小括弧中的文字 , 代表本练习的难度等级(用1~10表示) 。最后 , 在您阅读本书时 , 我有两点建议:? 对经典的分析、代码及时做批注 , 多做对比和总结 。? 上机编写运行书中的经典实例 , 尝试不同的运行结果 , 分析代码的用意 。? 对书中的实例举一反三、反覆推敲 , 分析更多的实际套用场景 。只有用心去读、去体会、去实践 , 才能够真正理解Java编程中的思想 。本评注版由刘中兵、陈景春、周同、刘中敏、陈万珍、刘剑、李瑞霞、李建海、李金刚、刘中丽执笔 , 撰写相关中文评注 。行文若有不妥之处 , 敬请广大读者提出宝贵意见和建议 。刘中兵2011年4月20日于北京目 录第1部分 基本语法Operators(新增批注30条) 1Simpler print statements 1Using Java operators 2Precedence 2Assignment 3Mathematical operators 4Unary minus and plus operators 6Auto increment and decrement 6Relational operators 7Testing object equivalence 7Logical operators 9Literals 10Exponential notation 11Bitwise operators 12Shift operators 13Ternary if-else operator 16String operator + and += 17Common pitfalls when usingoperators 18Casting operators 18Truncation and rounding 19Promotion 20Java has no “sizeof ” 20Summary 20Controlling Execution(新增批注21条) 21true and false 21if-else 21Iteration 22do-while 23for 23The comma operator 24Foreach syntax 25return 27 break and continue 27The infamous “goto” 29switch 32Summary 34第2部分 面向对象Initialization & Cleanup(新增批注55条) 35Guaranteed initialization withthe constructor 35Method overloading 37Distinguishing overloadedmethods 39Overloading with primitives 39Overloading on return values 42Default constructors 43The this keyword 44Calling constructors fromconstructors 46The meaning of static 47Cleanup: finalization andgarbage collection 47What is finalize() for? 48You must perform cleanup 49The termination condition 50How a garbage collector works 51Member initialization 54Specifying initialization 55Constructor initialization 56Order of initialization 56static data initialization 57Explicit static initialization 59Non-static instance initialization 61Array initialization 62Variable argument lists 65Enumerated types 70Summary 72Access Control(新增批注21条) 73package: the library unit 74Code organization 75Creating unique package names 76A custom tool library 79Java access specifiers 80Package access 80public: interface access 81 private: you can’t touch that! 82protected: inheritance access 83Interface and implementation 85Class access 86Summary 87Reusing Classes(新增批注35条) 89Composition syntax 89Inheritance syntax 92Initializing the base class 94Delegation 96Combining composition andinheritance 97Guaranteeing proper cleanup 99Name hiding 101Choosing composition vs.inheritance 103protected 104Upcasting 105Why “upcasting”? 106Composition vs. inheritancerevisited 106The final keyword 107final data 107final methods 110final classes 112final caution 113Initialization and classloading 113Initialization with inheritance 114Summary 115Interfaces(新增批注16条) 117Abstract classes and methods 117Interfaces 120“Multiple inheritance” in Java 123Extending an interface withinheritance 125Name collisions when combininginterfaces 127Fields in interfaces 127Initializing fields in interfaces 128Interfaces and factories 129Summary 130Inner Classes(新增批注32条) 131Creating inner classes 131The link to the outer class 133Using .this and .new 134Inner classes and upcasting 135Anonymous inner classes 137Factory Method revisited 140 Nested classes 142Classes inside interfaces 143Reaching outward from amultiply nested class 145Why inner classes? 145Closures & callbacks 148Inheriting from inner classes 150Can inner classes beoverridden? 150Local inner classes 152Inner-class identifiers 153Summary 154Error Handling with Excep- tions(新增批注52条) 155Basic exceptions 155Exception arguments 156Catching an exception 157The try block 157Exception handlers . 157Creating your ownexceptions 159Exceptions and logging 161The exception specification 164Catching any exception 164The stack trace 166Rethrowing an exception 167Exception chaining 169Standard Java exceptions 172Special case: RuntimeException 172Performing cleanup withfinally 174What’s finally for? 175Using finally during return 177Pitfall: the lost exception 178Exception restrictions 180Constructors 182Exception matching 187Alternative approaches 188Passing exceptions to the console 189Summary 189第3部分 数据存储Strings(新增批注53条) 191Immutable Strings 191Overloading ‘+’ vs.StringBuilder 192Unintended recursion 195 Operations on Strings 196Formatting output 199printf() 199System.out.format() 199The Formatter class 200Format specifiers 200Formatter conversions 202String.format() 204Regular expressions 205Basics 206Creating regular expressions 208Quantifiers 210Pattern and Matcher 211split() 218Replace operations 218reset() 220Regular expressions and Java I/O 221Scanning input 222Scanner delimiters 224Scanning with regularexpressions 225StringTokenizer 225Summary 226Arrays(新增批注36条) 227Why arrays are special 227Arrays are first-class objects 228Returning an array 231Multidimensional arrays 232Arrays and generics 235Creating test data 237Arrays.fill() 238Data Generators 239Arrays utilities 243Copying an array 243Comparing arrays 244Array element comparisons 245Sorting an array 248Searching a sorted array 249Summary 251Holding Your Objects(新增批注35条) 253Generics and type-safecontainers 254Basic concepts 256Adding groups of elements 258Printing containers 259List 261 Iterator 263ListIterator 266LinkedList 267Stack 268Set 270Map 273Queue 276PriorityQueue 277Collection vs. Iterator 279Foreach and iterators 281The Adapter Method idiom 283Summary 286Containers in Depth(新增批注102条) 289Full container taxonomy 289Filling containers 290A Generator solution 291Map generators 292Collection functionality 294Optional operations 297Unsupported operations 298List functionality 300Sets and storage order 302SortedSet 306Queues 307Priority queues 308Deques 309Understanding Maps 310Performance 311SortedMap 314LinkedHashMap 315Hashing and hash codes 316Understanding hashCode() 319Hashing for speed 321Overriding hashCode() 324Choosing an implementation 329A performance test framework 330Choosing between Lists 333Microbenchmarking dangers 338Choosing between Sets 339Choosing between Maps 341Utilities 344Sorting and searching Lists 347Making a Collection or Mapunmodifiable 349Synchronizing a Collection orMap 350Holding references 351The WeakHashMap 353Java 1.0/1.1 containers 355 Vector & Enumeration 355Hashtable 356Stack 356BitSet 357Summary 359第4部分 核心功能I/O(新增批注89条) 361The File class 361A directory lister 361Directory utilities 364Checking for and creatingdirectories 369Input and output 370Types of InputStream 371Types of OutputStream 372Adding attributes and usefulinterfaces 373Reading from an InputStreamwith FilterInputStream 374Writing to an OutputStreamwith FilterOutputStream 375Readers & Writers 376Sources and sinks of data 377Modifying stream behavior 377Unchanged classes 378Off by itself:RandomAccessFile 379Typical uses of I/O streams 379Buffered input file 379Input from memory 380Formatted memory input 381Basic file output 382Storing and recovering data 383Reading and writingrandom-access files 385Piped streams 386File reading &writing utilities 386Reading binary files 389Standard I/O 389Reading from standard input 389Changing System.out to aPrintWriter 390Redirecting standard I/O 391Process control 391New I/O 393Converting data 396Fetching primitives 398 View buffers 399Data manipulation with buffers 403Buffer details 404Memory-mapped files 406File locking 409Compression 411Simple compression with GZIP 412Multifile storage with Zip 413Java ARchives (JARs) 415Object serialization 416Finding the class 419Controlling serialization 420Using persistence 427XML 432Summary 434Concurrency(新增批注117条) 435Basic threading 435Defining tasks 435The Thread class 436Using Executors 438Producing return values fromtasks 440Sleeping 442Priority 443Yielding 444Daemon threads 445Coding variations 446Joining a thread 450Catching exceptions 451Sharing resources 454Resolving shared resourcecontention 454Atomicity and volatility 457Atomic classes 460Critical sections 462Synchronizing on other objects 462Thread local storage 463Terminating tasks 464The ornamental garden 465Terminating when blocked 467Interruption 469Cooperation between tasks 475wait() and notifyAll() 475notify() vs. notifyAll() 479Producers and consumers 482Producer-consumers and queues 486Using pipes for I/O between tasks 491 Deadlock 492New library components 497CountDownLatch 497CyclicBarrier 499DelayQueue 501PriorityBlockingQueue 503The greenhouse controller withScheduledExecutor 505Semaphore 508Exchanger 511Simulation 513Bank teller simulation 513The restaurant simulation 517Distributing work 521Performance tuning 526Comparing mutex technologies 526Lock-free containers 532ReadWriteLocks 533Active objects 535Summary 537第5部分 高级特性Type Information(新增批注59条) 539The need for RTTI 539The Class object 541Class literals 545Generic class references 547New cast syntax 549Checking before a cast 550Using class literals 555A dynamic instanceof 557Counting recursively 558Registered factories 559instanceof vs. Classequivalence 562Reflection: runtime classinformation 563A class method extractor 564Dynamic proxies 566Null Objects 570Mock Objects & Stubs 575Interfaces and typeinformation 576Summary 580Generics(新增批注126条) 583Comparison with C++ 584Simple generics 584A tuple library 586A stack class 588RandomList 589Generic interfaces 590 Generic methods 593Leveraging type argumentinference 594Varargs and generic methods 596A generic method to use withGenerators 596A general-purpose Generator 597Simplifying tuple use 598A Set utility 600Anonymous inner classes 603Building complex models 604The mystery of erasure 606The C++ approach 607Migration compatibility 609The problem with erasure 611The action at the boundaries 612Compensating for erasure 615Creating instances of types 616Arrays of generics 618Bounds 622Wildcards 625How smart is the compiler? 628Contravariance 629Unbounded wildcards 632Capture conversion 636Issues 637No primitives as type parameters 637Implementing parameterizedinterfaces 639Casting and warnings 640Overloading 641Base class hijacks an interface 642Self-bounded types 642Curiously-recurring generics 643Self-bounding 644Argument covariance 646Dynamic type safety 649Exceptions 650Mixins 651Mixins in C++ 651Mixing with interfaces 653Using the Decorator pattern 654Mixins with dynamic proxies 655Latent typing 657Compensating for the lack oflatent typing 660Reflection 661Applying a method to a sequence 662 When you don’t happento have the right interface 664Simulating latent typingwith adapters 665Using function objects asstrategies 668Summary: Is casting reallyso bad? 672Enumerated Types(新增批注55条) 675Basic enum features 675Using static imports with enums 676Adding methods to an enum 677Overriding enum methods 678enums in switch statements 678The mystery of values() 679Implements, not inherits 681Random selection 682Using interfaces fororganization 683Using EnumSet instead offlags 686Using EnumMap 688Constant-specific methods 689Chain of Responsibility withenums 692State machines with enums 695Multiple dispatching 700Dispatching with enums 702Using constant-specific methods 704Dispatching with EnumMaps 705Using a 2-D array 706Summary 707Annotations(新增批注51条) 709Basic syntax 710Defining annotations 710Meta-annotations 712Writing annotation processors 712Annotation elements 713Default value constraints 713Generating external files 714Annotations don’tsupport inheritance 717Implementing the processor 717Using apt to processannotations 719Using the Visitor pattern with apt 723Annotation-based unit testing 726Using @Unit with generics 733No “suites” necessary 735Implementing @Unit 735Removing test code 741Sum