标题：Java程序设计与开发
前言
Java语言源于Oak语言，这是SUN公司在一项消费性电子产品软件发展方案中采用的语言。Oak的设计目标是用以开发可靠、紧凑、易于移植的分布式嵌入系统。尽管Oak语言在技术上颇为成功，但由于商业上的原因，却未能在市场的激烈竞争中站稳脚跟。然而，Internet和WWW日新月异的发展却为Oak创造了新的生存空间。Oak经过改进，成为一种非常适合网络开发的独特语言——Java，从而脱颖而出，一举成名。 
Internet，中译为“因特网”或“互联网”，是当今最大的国际性计算机互联网络。它源于美国国防部高级研究计划局（Advanced Research Project Agency）的网络ARPANET，但发展到今天，已成为一个庞大的全球网。顾名思义，这是一个“网间网”它把各种异构网络联接一起，使位于世界各地、不同网络中的人们能够自由地交换信息。Internet在全球的用户以千万计，并在不断激增；被它直接、间接连通的国家也越来越多。Internet上的协议标准也日益受到广泛支持并在不断地得到扩充。
WWW全称为World Wide Web，中译为“万维网”、“全球网”等，是Internet中最为直观和富有魅力的资源。它运用超文本（Hypertext）技术将信息组织成文字、声音、图像，用户可以用浏览器读取这些信息。WWW上信息种类繁多，用户可以根据自己的需要在任何时间访问自己所需的信息。WWW上的信息由页面组成，页面中包含超文本链接(Hypertext Link)，把位于不同页面的相关内容链在一起，因而检索信息非常方便。WWW的发展也是日新月异，它已不止局限于展示静止信息，正在不断增强交互和动态性。许多商家和企业也把目光瞄准了WWW，可以预料，WWW世界将变得越来越丰富多彩。
Internet(含WWW)为人们提供了许多有用的信息，然而，Internet上的用户使用着各种各样的计算机，系统软件和用户软件也缺乏兼容性。在这些不兼容的平台间交换信息，就像对许多语言不通的人讲话一样，是非常困难的。而Java与生俱来的特性刚好可以解决这道电脑界的“世纪难题”。它建立的虚拟环境运行标准解决了软件跨平台执行的问题，使不同的平台都能理解用Java编写的程序，命名Java语言成了网络世界的通用语言。因此，Java在Internet和WWW环境下如鱼得水，迅速风靡全球。
总之，Java作为一种独特的语言，诞生于Internet和WWW蓬勃发展的时期，可谓恰逢其时。如今的Java与当初的Oak语言已不可同日而语。而且，Java技术的创始者SUN公司采用了一种“开放”策略，将它公诸于众，更激发了第三方软件开发商的极大热情，纷纷发展Java技术。Java的前景是非常广阔的。
读者对象
本书可作为高等学校计算机及相关专业“Java程序设计”课程的教材，也可作为软件开发人员、工程技术人员及其他相关人员的自学参考书或培训教程。
本书特色
本书着重介绍精简的Java语言概念、原理和Java技术，突出Java的实际开发应用技术，并结合到实践教学中去。
本书分层次讲解Java语言和Java技术两大分支，囊括了部分Java SE和Java EE的内容。以大量实例程序体现Java深层次应用。
本书在介绍Java程序设计的同时，也介绍Java开发技术的教材。对学生来说，可起到入门的作用。
前言
随着互联网的普及和发展，面向Web的程序设计和开发已成为软件编程的新模式，而Java日渐成为互联网编程的一种主流语言之一。自20世纪90年代早期以来，Java在IT业的应用一直保持增长的趋势。高等院校的计算机和非计算机专业相继开设了Java程序设计课程。
本书的目的是使学生在理解Java语法特点的基础上，逐步掌握Java语言面向对象的编程要领，同时鼓励学生利用面向对象编程来解决实际问题。
本书以Java语言面向对象的编程理念为出发点，循序渐进地介绍Java语言的基本内容，包括基本语法、数组、字符串、类、抽象类、接口、网络和多线程编程；同时还着重介绍几种Java核心技术，包括JDBC、Servlet、JSP和EJB。通过融入Java面向对象的编程思想，使读者在学习理论知识的同时，初步掌握面向对象的系统分析、设计与开发能力，为大型软件的设计和开发奠定基础。本书在讲解Java语言和Java核心技术的同时，辅以大量的实例和结果分析帮助读者快速理解概念的本质，进一步掌握Java语言及其技术的精髓。本书在实例的安排上，由点到面、由浅入深，并在面向对象的设计思想和技巧上进行深入思考和探讨。
本书第1章主要介绍Java语言的特点和优势，使读者对Java语言有一个概括性的了解。第2章介绍Java语言基础，主要包括数据类型、变量、表达式、流程控制语句、数组和字符串。第3章介绍Java类和对象，包括Java类的设计，对象创建和构造方法的执行过程；多态性，类、变量和方法的修饰符(访问和非访问修饰符)；内部类，抽象类和接口，包及其作用。第4章介绍Java的I/O流和异常处理，包括System I/O类、Scanner类、File类、字节级输入输出流、字符级输入输出流、对象序列化、异常捕获和处理。第5章介绍Java图形用户界面和Applet，使用AWT包和Swing包生成图形用户界面，同时使用多种布局管理器设计友好的用户界面。第6章介绍Java网络编程，包括基于高层URL、基于套接字Socket和基于数据报Datagram等3种编程方式，以及使用文件I/O流辅助网络编程。第7章介绍Java多线程编程，包括线程的创建和启动、线程的优先级、状态切换和多线程的同步处理。第8章介绍Java数据库连接技术(JDBC)，以及利用JDBC编写数据库访问程序的步骤。第9章介绍Servlet和JSP，包括Java EE Web开发概念，JavaBean，Servlet概念及其生命周期，Servlet类的编写、配置和运行，JSP及其编写、配置和运行，JSP的两种设计模式。第10章介绍RMI和EJB技术，利用RMI机制设计RMI客户端和服务器程序的步骤，介绍了EJB规范、会话类EJB组件的创建和部署，以及编写EJB客户端程序。
本书配有PPT、源代码、习题答案等教学资源，需要者可从华信教育资源网免费注册下载。
本书适合作为计算机及相关专业的本科生、研究生“Java程序设计”课程的教材，也可作为软件开发人员、工程技术人员及其他相关人员作为自学参考书或培训教程。
本书由张晓龙任主编。其中，第1、3、5章由张晓龙编写；第2、4章由赵红武编写；第6、7章由刘钊编写；第8、9、10章由边小勇编写。书中的所有实例都在JDK 1.6中调试通过。本书编写过程中，得到了武汉科技大学计算机学院人工智能与机器学习研究室成员的大力支持，在此，谨向他们表示衷心的感谢。
……
目录
第1章 Java语言概述······························· (1)
1.1 Java语言及其产生···························· (1)
1.2 Java的特点································ (1)
1.3 Java的体系结构····························· (3)
1.3.1 Java的体系结构···························· (3)
1.3.2 Java虚拟机······························ (3)
1.3.3 垃圾回收机制····························· (3)
1.3.4 代码安全检测····························· (4)
1.4 Java的3种版本······························ (4)
1.5 Java开发环境概述···························· (4)
1.5.1 JDK的安装、配置和使用························ (4)
1.5.2 JDK工具箱····························· (6)
1.5.3 Eclipse开发环境的安装、配置和使用··················· (6)
1.5.4 Java程序的编写···························· (7)
1.6 本章小结································· (9)
习题1···································· (9)
第2章 Java语言基础······························ (11)
2.1 Java语言要素······························ (11)
2.1.1 标识符······························· (11)
2.1.2 关键字······························· (11)
2.1.3 分隔符······························· (12)
2.1.4 注释································ (12)
2.2 基本数据类型······························ (13)
2.2.1 简单类型······························ (13)
2.2.2 引用类型······························ (14)
2.2.3 常量································ (14)
2.2.4 变量································ (15)
2.3 运算符和表达式····························· (16)
2.3.1 算术运算符····························· (16)
2.3.2 关系运算符····························· (17)
2.3.3 逻辑运算符····························· (17)
2.3.4 位运算符······························ (18)
2.3.5 赋值运算符····························· (19)
2.3.6 其他运算符····························· (20)
2.3.7 运算符的优先级与结合性························ (21)
2.3.8 表达式与语句···························· (21)
2.4 控制语句································ (22)
2.4.1 选择结构控制语句·························· (23)
2.4.2 循环结构控制语句·························· (25)
2.4.3 转移语句······························ (27)
2.5 数组·································· (29)
2.5.1 一维数组······························ (29)
2.5.2 二维数组······························ (31)
2.5.3 多维数组······························ (32)
2.6 字符串································· (32)
2.6.1 字符串类······························ (33)
2.6.2 常用方法······························ (33)
2.7 综合应用实例······························ (34)
2.8 本章小结································ (37)
习题2···································· (37)
第3章 Java类和对象······························ (39)
3.1 面向对象基础······························ (39)
3.1.1 类································· (39)
3.1.2 对象································ (40)
3.1.3 消息································ (40)
3.2 继承·································· (40)
3.2.1 创建子类······························ (40)
3.2.2 成员变量和方法··························· (41)
3.2.3 对象的构造和初始化过程························ (44)
3.2.4 上转型对象····························· (45)
3.2.5 关键字this和super·························· (47)
3.2.6 对象的撤销和清理·························· (49)
3.3 多态·································· (50)
3.3.1 基本概念······························ (50)
3.3.2 方法重载······························ (50)
3.3.3 方法覆盖······························ (51)
3.3.4 变量覆盖······························ (53)
3.4 修饰符································· (53)
3.4.1 访问修饰符····························· (53)
3.4.2 非访问修饰符···························· (57)
3.5 内部类和匿名内部类··························· (59)
3.5.1 内部类······························· (59)
3.5.2 匿名内部类····························· (61)
3.6 接口·································· (63)
3.6.1 接口的定义····························· (63)
3.6.2 接口与多重继承··························· (63)
3.6.3 接口的实现····························· (64)
3.6.4 接口中的变量和方法························· (64)
3.7 包··································· (66)
3.7.1 包的作用······························ (66)
3.7.2 Java中定义的包··························· (66)
3.7.3 自定义包的创建和使用························ (66)
3.7.4 编译和生成包···························· (67)
3.8 综合应用实例······························ (68)
3.9 本章小结································ (70)
习题3···································· (71)
第4章 Java的I/O流和异常处理·························· (73)
4.1 I/O流概述································ (73)
4.2 System I/O类和Scanner类························· (75)
4.2.1 System I/O类···························· (75)
4.2.2 Scanner类······························ (76)
4.3 文件类································· (76)
4.3.1 使用File类访问文件系统························ (76)
4.3.2 随机文件读写···························· (78)
4.4 字节级输入输出流···························· (79)
4.4.1 数据输入输出流··························· (79)
4.4.2 文件输入输出流··························· (80)
4.4.3 缓冲输入输出流··························· (81)
4.4.4 管道输入输出流··························· (81)
4.4.5 格式化输出流···························· (81)
4.5 字符级输入输出类···························· (82)
4.5.1 字符输入输出类··························· (82)
4.5.2 缓冲读写类····························· (83)
4.5.3 文件读写类····························· (86)
4.5.4 格式化写类····························· (88)
4.6 对象序列化······························· (89)
4.6.1 对象输入流····························· (89)
4.6.2 对象输出流····························· (90)
4.7 异常处理································ (91)
4.7.1 Java中定义的异常·························· (91)
4.7.2 异常类的层次结构·························· (92)
4.7.3 捕获异常······························ (92)
4.7.4 异常处理的嵌套··························· (96)
4.7.5 自定义的异常和使用························· (97)
4.8 文件流综合应用实例··························· (98)
4.9 本章小结································ (106)
习题4··································· (106)
第5章 Java用户界面类和Applet························· (108)
5.1 AWT简介······························· (108)
5.2 AWT容器类······························ (108)
5.3 基本控件类······························· (110)
5.4 图形类································· (112)
5.5 事件类型及其处理··························· (112)
5.5.1 事件类······························ (113)
5.5.2 事件监听器····························· (113)
5.5.3 事件处理流程···························· (114)
5.6 布局管理器······························· (115)
5.6.1 FlowLayout····························· (115)
5.6.2 BorderLayout···························· (116)
5.6.3 GridLayout····························· (116)
5.6.4 CardLayout····························· (116)
5.6.5 GridBagLayout··························· (117)
5.7 Swing简介······························· (117)
5.7.1 Swing组件的特点·························· (118)
5.7.2 Swing包中的类··························· (118)
5.7.3 Swing包中的控件类························· (118)
5.8 Java Applet······························· (122)
5.8.1 Applet简介···························· (122)
5.8.2 Applet程序的编写·························· (124)
5.8.3 Applet程序的编译和执行······················· (125)
5.8.4 Applet的绘图和控制························· (126)
5.9 GUI设计综合应用实例·························· (128)
5.10 本章小结······························· (130)
习题5··································· (130)
第6章 Java网络编程······························ (132)
6.1 Java网络基础······························ (132)
6.1.1 IP地址······························ (132)
6.1.2 端口······························· (133)
6.1.3 套接字······························ (134)
6.1.4 数据报······························ (134)
6.1.5 传输协议······························ (135)
6.2 基于URL的高层次Java网络编程····················· (135)
6.2.1 从URL中读取内容························· (135)
6.2.2 建立URL连接并读取内容······················· (136)
6.3 基于Socket的网络编程························· (137)
6.3.1 Socket通信过程··························· (138)
6.3.2 创建客户端Socket·························· (139)
6.3.3 创建服务器端Socket························· (139)
6.3.4 打开输入输出流··························· (139)
6.3.5 关闭流和Socket··························· (141)
6.4 基于Datagram的网络编程························ (141)
6.4.1 数据报和套接字··························· (142)
6.4.2 创建客户端Datagram························· (142)
6.4.3 创建服务器端Datagram························ (142)
6.4.4 打开输入输出流··························· (143)
6.4.5 关闭流和Datagram·························· (143)
6.5 网络编程与应用实例·························· (144)
6.6 本章小结································ (150)
习题6··································· (150)
第7章 Java多线程编程····························· (152)
7.1 线程基础································ (152)
7.1.1 线程概述······························ (152)
7.1.2 线程的状态····························· (152)
7.1.3 线程的优先级···························· (153)
7.2 线程的创建······························· (153)
7.2.1 通过继承Thread类创建························ (153)
7.2.2 通过实现Runnable接口创建······················ (155)
7.3 多线程的创建······························ (156)
7.4 线程的调度······························· (159)
7.4.1 线程优先级的获取与设置······················· (159)
7.4.2 线程睡眠······························ (159)
7.4.3 线程让步······························ (159)
7.4.4 线程等待······························ (160)
7.4.5 线程唤醒······························ (160)
7.5 多线程的同步处理··························· (161)
7.5.1 多线程的互斥···························· (161)
7.5.2 synchronized方法·························· (161)
7.6 多线程综合应用实例·························· (164)
7.7 本章小结································ (168)
习题7··································· (168)
第8章 数据库编程······························· (170)
8.1 数据库基础······························· (170)
8.1.1 关系数据库····························· (170)
8.1.2 SQL基本内容···························· (171)
8.1.3 ODBC技术····························· (173)
8.2 JDBC数据库连接技术·························· (174)
8.2.1 JDBC概述····························· (174)
8.2.2 JDBC的实现及其驱动程序······················ (174)
8.2.3 JDBC的常用类和接口························ (175)
8.3 Java访问数据库的基本步骤······················· (176)
8.3.1 加载驱动程序类··························· (176)
8.3.2 建立JDBC连接··························· (177)
8.3.3 执行SQL语句··························· (179)
8.4 JDBC高级特性····························· (180)
8.4.1 预备语句······························ (180)
8.4.2 可滚动和可更新的结果集······················· (180)
8.4.3 元数据······························ (182)
8.4.4 事务······························· (183)
8.5 创建数据库······························· (184)
8.6 JDBC综合应用举例··························· (185)
8.7 本章小结································ (187)
习题8··································· (188)
第9章 Servlet与JSP······························ (189)
9.1 Java EE Web开发技术·························· (189)
9.2 Servlet基础······························· (190)
9.2.1 Servlet定义····························· (190)
9.2.2 Servlet工作方式··························· (191)
9.2.3 Servlet API····························· (192)
9.2.4 Servlet异常····························· (193)
9.3 配置Servlet工作环境·························· (193)
9.3.1 Servlet开发环境··························· (193)
9.3.2 Servlet类的配置··························· (194)
9.3.3 编写简单的Servlet·························· (195)
9.4 JavaBean基础······························ (196)
9.4.1 JavaBean基本概念·························· (196)
9.4.2 JavaBean的属性、方法和事件····················· (197)
9.4.3 一个简单的JavaBean························· (197)
9.5 JSP概述································ (198)
9.5.1 JSP基本概念···························· (198)
9.5.2 JSP技术的特点··························· (199)
9.5.3 JSP的工作方式··························· (199)
9.5.4 JSP和Servlet的区别························· (200)
9.6 编写简单的JSP····························· (200)
9.7 在JSP中访问数据库··························· (200)
9.8 JSP的设计模式····························· (202)
9.8.1 JavaBean与JSP的结合························ (202)
9.8.2 Servlet与JSP的结合························· (203)
9.9 JSP的语法要素····························· (203)
9.9.1 JSP脚本元素···························· (203)
9.9.2 JSP指令和动作元素························· (205)
9.9.3 JSP内置对象···························· (206)
9.10 Web综合应用实例··························· (209)
9.11 本章小结······························· (222)
习题9··································· (223)
第10章 RMI和EJB······························ (224)
10.1 RMI基础······························· (224)
10.1.1 RMI概述···························· (224)
10.1.2 RMI的目标和体系结构······················· (224)
10.1.3 RMI的工作机制·························· (225)
10.1.4 用RMI设计应用系统························ (226)
10.2 RMI API································ (227)
10.3 Java RMI与其他分布式技术······················· (228)
10.4 RMI部署问题····························· (229)
10.5 编写一个RMI应用系统························· (229)
10.6 EJB技术································ (233)
10.6.1 EJB基本概念··························· (233)
10.6.2 EJB组件····························· (234)
10.6.3 EJB容器····························· (235)
10.6.4 开发和部署EJB·························· (236)
10.6.5 EJB和其他分布式技术······················· (237)
10.6.6 基于EJB的Web应用程序实例···················· (238)
10.7 本章小结······························· (244)
习题10··································· (245)
附录A Java运算符及其优先级·························· (246)
附录B Java类路径和类加载器·························· (247)
参考文献···································· (249)