前言
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)