Chapter 1 Introduction(简介) ...................................... 1 1.1 Characteristics of Java(Java 的特点) ...............................................................1 1.2 Developing tools(开发工具).............................................................................21.2.1 Interpreted language vs compiled language(解释性语言与编译性语言).......... 2 1.2.2 Java Virtual Machine(虚拟机)............................................................................. 3 1.2.3 Java Runtime Environment(运行环境) ............................................................... 4 1.2.4 Java Development Kit(开发工具)....................................................................... 41.3 A simple Java program(一个简单的 Java 程序)................................................5 1.3.1 How to run a Java program?(如何运行 Java 程序?) ........................................ 5 1.3.2 Rules of naming a Java source file(Java 源文件的命名规则) ........................... 6 1.3.3 Rules of naming classes, variables and methods(类、变量和方法的命名规则)........ 7Exercises ......................................................................................................................8Chapter 2 Java Foundation(Java 基础) .............................. 9 2.1 Primitive data types(基本数据类型).................................................................9 2.1.1 Integer and floating point types(整型与浮点型)............................................... 10 2.1.2 Boolean type(布尔型).........................................................................................11 2.1.3 Character type(字符型) ......................................................................................11 2.2 Reference types(引用型)...............................................................................12 2.2.1 A class is a data type(类是数据类型) ............................................................... 13 2.2.2 A class type variable is a reference(类类型的变量是引用) ............................. 14 2.2.3 Interface type(接口类型)................................................................................... 16 2.3 Identifiers(标识符) .........................................................................................16 2.4 Default values of fields(成员变量的默认值)...................................................16 2.5 Where data store?(数据存储在何处?) .........................................................18 2.6 Operators(运算符) .........................................................................................19 2.6.1 Arithmetic operators(算术运算符).................................................................... 19 2.6.2 Logical operators(逻辑运算符)......................................................................... 192.6.3 Bitwise operators(位运算符)............................................................................. 20 2.6.4 Left shift (>) operators(移位运算符)................... 21 2.6.5 Assignment and conditional operators(赋值运算符和条件运算符) ................ 22 2.6.6 String operator “ ” and “ =”(字符串运算符“ ”和“ =”) .............. 22 2.6.7 Special operators(特殊运算符) ......................................................................... 232.7 Casting(类型转换) .........................................................................................23 2.7.1 Widening and narrowing(拓宽与缩窄)............................................................. 23 2.7.2 Char, byte and short produce int results(Char, byte 和 short 转换为 int 型) ......... 252.8 Flowing control(流程控制) .............................................................................25 2.8.1 Basic controlling statements(基本控制语句) ................................................... 26 2.8.2 Foreach statement(foreach 语句)....................................................................... 262.9 Arrays(数组) ..................................................................................................28 2.9.1 Define arrays(定义数组)................................................................................... 28 2.9.2 Initialize arrays(初始化数组)............................................................................ 28 2.9.3 Arrays act as arguments of methods(数组做方法的参数) ............................... 292.10 Command line arguments(命令行参数).......................................................30 Exercises ....................................................................................................................31Chapter 3 Classes and Objects(类和对象).......................... 333.1 Concepts of OOP(面向对象的概念) ..............................................................333.1.1 Everything is an Object(万物皆对象)............................................................... 333.1.2 Defining classes(定义类) .................................................................................. 343.2 Useful classes(常用类)..................................................................................343.3 Method overloading(方法重载)......................................................................393.4 Constructors(构造方法) .................................................................................403.5 Default constructor(默认构造方法) ................................................................433.6 Static fields and methods(静态成员变量与静态方法) ....................................453.7 This keyword(this 关键字)..............................................................................513.7.1 A non-static method has a hidden “this”(隐藏参数 this 的非静态方法) ......... 513.7.2 A static method has no argument “this”(没有 this 的静态方法).................. 543.7.3 Calling constructors form constructors(在构造方法里调用其他构造方法) ....... 563.8 Variable argument lists(可变参数列表)..........................................................563.9 Garbage collection(垃圾回收) .......................................................................583.10 Enum type(枚举类型) ..................................................................................60Exercises ....................................................................................................................61Chapter 4 Packages(包)........................................... 64 4.1 Concept of packages(包的概念)....................................................................64 4.2 Java library and its package structure(类库与 Java 类的包组织结构) ............65 4.3 Create packages(创建包)..............................................................................66 4.4 Import packages(导入包) ..............................................................................68 4.5 Package java.lang(java.lang 包).....................................................................70 4.6 Useful classes in package java.lang(java.lang 包中常用的类) .......................714.6.1 Object class and its toString method(Object 类和它的 toString 方法) ............ 714.6.2 System class(系统类) ........................................................................................ 72 Exercises ....................................................................................................................72Chapter 5 Inheritance(继承) ...................................... 74 5.1 What is inheritance?(什么是继承?)..............................................................74 5.1.1 Root class Object(根类对象)............................................................................. 75 5.1.2 Defining subclasses(定义子类) ........................................................................ 76 5.2 Super keyword(super 关键字) ........................................................................78 5.2.1 Super corresponding to default constructor(默认构造方法的 super)............... 78 5.2.2 Super corresponding to constructors with arguments(有参构造方法的 super) ....... 80 5.3 Order of constructor calls(构造方法的调用次序) ...........................................82 5.4 Final keyword(final 关键字)............................................................................84 5.4.1 Final fields(final 成员) ..................................................................................... 84 5.4.2 Final arguments(常参数)................................................................................... 86 5.4.3 Final methods(常方法)...................................................................................... 86 5.5 Access specifiers(访问说明符) ......................................................................86 5.6 Polymorphism(多态)......................................................................................91 5.6.1 Method overriding(方法重写)........................................................................... 92 5.6.2 Upcasting and dynamic polymorphism(升级转换与动态多态)....................... 935.6.3 Referring to a member of the super class by super keyword(用 super 指向 基类成员).............................................................................................................. 96 5.6.4 Hiding fields and static methods of the base class(隐藏静态方法和 fields 的基类) ....... 98 Exercises ..................................................................................................................100Chapter 6 Abstract Class and Interface(抽象类和接口) ............ 104 6.1 Abstract class(抽象类).................................................................................105 6.1.1 Abstract method(抽象方法) ............................................................................ 1056.1.2 Abstract class(抽象类) .................................................................................... 105 6.2 Interface(接口).............................................................................................106 6.2.1 Introduction(简介)........................................................................................... 106 6.2.2 Defining interfaces(接口的定义).................................................................... 107 6.2.3 Implementation of interfaces(接口的实现)..................................................... 108 6.2.4 Comparation of interfaces and abstract classes(接口与抽象类的比较)......... 109 6.3 Inner class(内部类)......................................................................................110 6.3.1 Members in inner class(内部类成员)...............................................................110 6.3.2 Local inner class(局部内部类).........................................................................112 6.3.3 Anonymous inner class(匿名内部类) ..............................................................113 Exercises .................................................................................................................. 115Chapter 7 Generics and Collections(泛型与集合)..................117 7.1 Generics(泛型) ............................................................................................ 117 7.1.1 Concept of generics(泛型的概念)....................................................................117 7.1.2 Generic classes(泛型类)...................................................................................1187.1.3 Type parameters use “extends” and “super” keywords(类型参数中使用 extends 和 super 关键字) .......................................................................... 121 7.1.4 Wildcard in type parameters(类型参数里的通配符) ..................................... 122 7.2 Generic interfaces(泛型接口) ......................................................................122 7.3 Generic methods(泛型方法) ........................................................................122 7.4 Collection classes(集合类) ..........................................................................123 7.4.1 Concept of collection (container)classes[集合(容器)类的概念]............... 124 7.4.2 The hierarchy of the collection framework(集合框架的层次结构)............... 124 7.5 List(列表) .....................................................................................................126 7.6 Queue(队列) ................................................................................................ 131 7.7 Set(集合) .....................................................................................................131 7.8 Map(映射)....................................................................................................133 Exercises ..................................................................................................................135Chapter 8 Exception Handing(异常处理)..........................137 8.1 Concepts of exception(异常的概念) ............................................................137 8.1.1 What is an exception?(什么是异常?) ............................................................. 137 8.1.2 How to deal with exceptions?(如何处理异常?) ............................................. 138 8.2 Exception classes(异常类) ..........................................................................1408.2.1 Error class(Error 类) ........................................................................................ 1418.2.2 Exception class(Exception 类)......................................................................... 141 8.3 Catch and deal with an exception(捕获与处理异常)....................................144 8.4 Throw an exception(抛出一个异常).............................................................148 8.5 Define your own exceptions(用户自定义异常类).........................................152 Exercises ..................................................................................................................153Chapter 9 I/O(输入/输出)........................................155 9.1 Concept of I/O stream(输入 / 输出流的概念) ...............................................155 9.2 Byte stream(字节流)....................................................................................1569.2.1 InputStream/OutputStream(InputStream/OutputStream 类) ........................... 157 9.2.2 FileInputStream/FileOutputStream(FileInputStream/FileOutputStream 类)........ 159 9.2.3 FilterInputStream/FilterOutputStream(FilterInputStream/FilterOutputStream 类)....................................................................................... 163 9.2.4 DataInputStream/DataOutputStream(DataInputStream/DataOutputStream 类)........................................................................................ 164 9.2.5 BufferedInputStream/BufferedOutputStream(BufferedInputStream/BufferedOutputStream 类) ................................................................................. 165 9.2.6 PrintStream(PrintStream 类)............................................................................ 166 9.3 Character streams(字符流) .........................................................................169 9.3.1 Text file vs Binary file(文本文件与二进制文件).......................................... 170 9.3.2 Reader/Writer(Reader/Writer 类) .................................................................... 1719.3.3 InputStreamReader/OutputStreamWriter(InputStreamReader/ OutputStreamWriter 类) ..................................................................................... 172 9.3.4 FileReader/FileWriter(FileReader/FileWriter 类) ........................................... 175 9.3.5 BufferReader/BufferWriter(BufferReader/BufferWriter 类)........................... 178 9.3.6 PrintWriter(PrintWriter 类).............................................................................. 181 9.4 File class(文件类) ........................................................................................181 Exercises ..................................................................................................................184Chapter 10 Multi-threading(多线程) .............................. 186 10.1 Concept of multi-threading(多线程的概念) ................................................186 10.1.1 What’s a thread?(什么是线程?) ................................................................. 186 10.1.2 Thread vs process(线程与进程).................................................................... 186 10.2 Life cycle of a thread(线程的生命周期) .....................................................18810.3 Creating threads(创建线程).......................................................................190 10.3.1 Direct approach of creating a thread(直接法创建线程) ............................... 190 10.3.2 Indirect approach of creating a thread(间接法创建线程)............................. 19210.4 Main thread(主线程) ..................................................................................194 10.5 Methods of Thread class(线程类的方法)...................................................196 10.6 Thread synchronization(线程同步) ............................................................199 10.7 Communication between threads(线程间的通信)......................................202 Exercises ..................................................................................................................203Chapter 11 Networking(网络) .................................... 205 11.1 Concept of networking(网络的概念) ..........................................................205 11.2 URL class(URL 类) ....................................................................................206 11.3 Sockets communication(套接字通信) ........................................................20911.3.1 How Socket communication works?(套接字通信是如何进行的?)........... 20911.3.2 Ports(端口)..................................................................................................... 210 11.4 ServerSocket and Socket classes(ServerSocket 和 Socket 类)....................210 11.4.1 Tasks of each side(双方的任务).....................................................................211 11.4.2 Data transmission(数据传输) ........................................................................ 212 11.5 Serving multiple clients(服务多个客户)......................................................217 Exercises ..................................................................................................................219References .......................................................... 222