主要內容
本書具有知識全面、內容翔實、講解詳細及實例豐富等特點。書中所有知識都結合具體實例進行講解,理論聯繫實際套用,可以幫助學習者輕鬆掌握Java 程式開發的知識、方法和技巧,快速提升開發技能。為了幫助學習者隨時掃除學習障礙,本書還增設了“e 學碼”,不易理解的專業術語、代碼難點只需用手機掃一掃,即可獲得更多擴展解釋。此外,掃描封面的官方公眾號二維碼即可獲得本書程式源碼,登錄明日學院網站還可以獲得更多學習資源和技術支持。
本書是為不同層次的Java 程式設計師精心設計,既適合初學者入門,又適合程式設計師進階,同時還可以作為高等院校講授面向對象程式設計語言以及Java 語言的參考教材。
目錄
第1 章初識Java.....................................1
1.1Java簡介.............................................1
█ 1.1.1什麼是Java...................................1
█ 1.1.2Java的版本....................................1
█ 1.1.3JavaAPI文檔..................................2
1.2搭建Java開發環境.....................................2
█ 1.2.1JDK的下載與安裝...............................3
█ 1.2.2JDK的配置.....................................4
█ 1.2.3Eclipse的下載與啟動...........................7
█ 1.2.4Eclipse工作檯.................................9
█ 1.2.5導入項目檔案.................................10
1.3 使用Eclipse創建Java項目.............................. 11
█ 1.3.1創建Java項目.................................11
█ 1.3.2創建Java類檔案................................13
█ 1.3.3使用編輯器編寫程式代碼...........................14
█ 1.3.4運行Java程式.................................16
1.4程式調試.............................................16
1.5 Eclipse漢化...........................................18
1.6 知識回顧.............................................19
第2 章Java編碼基礎.............................20
2.1編碼須知..............................................20
█ 2.1.1Java代碼的編寫要求.............................20
█ 2.1.2代碼注釋......................................21
2.2 控制台的輸入和輸出操作................................22
█ 2.2.1控制台輸出字元................................22
█ 2.2.2控制台輸入字元................................23
2.3 知識回顧..............................................28]
第3 章基本數據類型.................................29
3.1 變數與常量.............................................29
█ 3.1.1標識符與關鍵字.................................29
█ 3.1.2變數...........................................30
█ 3.1.3常量...........................................31
3.2 基本數據類型...........................................32
█ 3.2.1整數類型.......................................32
█ 3.2.2浮點類型.......................................34
█ 3.2.3字元類型.......................................36
█ 3.2.4布爾類型.......................................38
3.3 數據類型轉換...........................................39
█ 3.3.1隱式轉換.......................................39
█ 3.3.2顯式轉換.......................................41
3.4 知識回顧...............................................42
第4 章運算符............................................43
4.1 賦值運算符..............................................43
4.2 算術運算符..............................................44
4.3 自增和自減運算符........................................45
4.4 關係運算符..............................................46
4.5 邏輯運算符..............................................47
4.6 位運算符...............................................48
█ 4.6.1位邏輯運算符....................................49
█ 4.6.2位移運算符......................................51
4.7複合賦值運算符...........................................54
4.8圓括號...................................................55
4.9三元運算符...............................................56
4.10instanceof運算符........................................56
4.11運算符優先權............................................56
4.12知識回顧................................................57
第5 章判斷語句............................................58
5.1決策分支..................................................58
5.2判斷語句..................................................59
█ 5.2.1if語句............................................59
█ 5.2.2if…else語句......................................61
█ 5.2.3if…elseif多分支語句.............................63
5.3三元運算符的使用..........................................64
5.4switch語句................................................66
5.5判斷語句嵌套..............................................70
5.6知識回顧..................................................72
第6 章循環語句............................................73
6.1while循環語句.............................................73
6.2do…while循環語句.........................................75
6.3while語句與do…while語句比較..............................77
6.4for循環語句...............................................79
█ 6.4.1for循環語句........................................79
█ 6.4.2foreach語句........................................82
6.5循環嵌套..................................................83
6.6循環控制..................................................84
█ 6.6.1break語句..........................................85
█ 6.6.2continue語句.......................................87
6.7知識回顧..................................................89
第7 章數組.....................................................90
7.1數組的概述................................................90
7.2一維數組..................................................90
█ 7.2.1創建一維數組........................................91
█ 7.2.2初始化一維數組.....................................92
█ 7.2.3獲取數組長度.......................................92
█ 7.2.4使用一維數組.......................................93
7.3二維數組..................................................94
█ 7.3.1創建二維數組........................................94
█ 7.3.2初始化二維數組......................................95
█ 7.3.3使用二維數組........................................96
7.4多維數組...................................................97
█ 7.4.1多維數組.............................................97
█ 7.4.2不規則數組...........................................99
7.5數組的基本操作.............................................100
█ 7.5.1遍歷數組.............................................100]
█ 7.5.2 批量填充或替換數組元素.................................100
█7.5.3複製數組..............................................102
7.6數組的排序...................................................104
█ 7.6.1算法:冒泡排序.........................................104
█ 7.6.2算法:直接選擇排序.....................................106
█ 7.6.3Arrays.sort()方法.......................................108
7.7 知識回顧......................................................109
第8 章字元串.....................................................110
8.1String類.....................................................110
█ 8.1.1聲明字元串........................................110
█ 8.1.2為字元串變數賦值....................................111
8.2 拼接字元串..................................................113
█ 8.2.1拼接多個字元串.....................................113
█ 8.2.2拼接其他數據類型...................................114
8.3 獲取字元串信息..............................................116
█ 8.3.1獲取字元串的長度...................................116
█ 8.3.2獲取指定位置的字元.................................117
█ 8.3.3 獲取子字元串索引位置..................................118
█ 8.3.4判斷字元串首尾內容.................................122
█ 8.3.5獲取字元數組.......................................125
█ 8.3.6獲取位元組數組.......................................126
█ 8.3.7判斷子字元串是否存在..............................126
8.4 字元串的操作................................................127
█ 8.4.1截取字元串.........................................127
█ 8.4.2字元串替換........................................129
█ 8.4.3字元串分割........................................131
█ 8.4.4大小寫轉換........................................136
█ 8.4.5去除空白內容.......................................137
█ 8.4.6比較字元串是否相同.................................138
8.5 字元串與基本數據類型的轉化..................................140
8.6 StringBuffer類.............................................144
█ 8.6.1創建StringBuffer類..................................145
█ 8.6.2StringBuffer類的常用方法.............................145
8.7 StringBuilder類.............................................149
█ 8.7.1StringBuilder類的使用方法...........................149
◆8.7.2StringBuffer、StringBuilder與String類之間的關係......150
8.8 正則表達式...................................................152
8.9 格式化字元串................................................154
8.10 知識回顧...................................................158
第9 章面向對象編程基礎...............................159
9.1面向對象概述................................................159
█ 9.1.1 對象...............................................159
█ 9.1.2 類................................................160
█ 9.1.3 面向對象程式設計的特點............................160
9.2類與對象....................................................162
█ 9.2.1 成員變數..........................................162
█ 9.2.2 成員方法..........................................163
█ 9.2.3 構造方法..........................................166
█ 9.2.4 局部變數..........................................168
█ 9.2.5 this關鍵字........................................169
9.3static關鍵字..................................................171
█ 9.3.1靜態變數.............................................171
█ 9.3.2靜態方法.............................................172
█ 9.3.3靜態代碼塊..........................................174
9.4類的主方法...................................................175
9.5知識回顧.....................................................177
第10 章面向對象核心技術...............................178
10.1類的封裝....................................................178
10.2類的繼承.....................................................182
█ 10.2.1extends關鍵字........................................183
█ 10.2.2方法的重寫..........................................184
█ 10.2.3 所有類的父類—Object類.............................187
10.3類的多態....................................................189
█ 10.3.1方法的重載.........................................189
█ 10.3.2向上轉型............................................191
█ 10.3.3向下轉型.............................................192
█ 10.3.4instanceof關鍵字....................................194
10.4抽象類與接口................................................195
█ 10.4.1抽象類與抽象方法....................................195
█ 10.4.2接口的聲明及實現....................................198
█ 10.4.3多重繼承............................................200
█ 10.4.4區分抽象類與接口....................................202
10.5訪問控制....................................................202
█ 10.5.1許可權修飾符.........................................203
█ 10.5.2Java類包.............................................204
█ 10.5.3final關鍵字..........................................205
10.6內部類......................................................210
█ 10.6.1成員內部類.........................................210
█ 10.6.2局部內部類.........................................215
█ 10.6.3匿名內部類.........................................216
█ 10.6.4靜態內部類.........................................217
█ 10.6.5內部類的繼承........................................218
10.7 知識回顧....................................................218
第11 章異常處理.............................................219
11.1異常概述.....................................................219
11.2異常的分類...................................................220
█ 11.2.1 系統錯誤—Error類.................................221
█ 11.2.2 異常—Exception...................................221
11.3捕捉處理異常..................................................224
█ 11.3.1try…catch代碼塊......................................225
█ 11.3.2finally代碼塊..........................................227
11.4在方法中拋出異常..............................................228
█ 11.4.1使用throws關鍵字拋出異常..............................228
█ 11.4.2使用throw關鍵字拋出異常...............................230
11.5自定義異常...................................................231
11.6異常的使用原則...............................................232
11.7知識回顧.....................................................233
第12 章Java常用類........................................234
12.1包裝類.......................................................234
V
█ 12.1.1Integer類................................................234
█ 12.1.2Double類.................................................237
█ 12.1.3Boolean類................................................239
█ 12.1.4Character類..............................................241
█ 12.1.5Number類.................................................242
12.2Math類...........................................................243
█ 12.2.1Math類概述...............................................243
█ 12.2.2常用數學運算方法.........................................244
12.3隨機數..........................................................248
█ 12.3.1Math.random()方法.......................................249
█ 12.3.2Random類................................................251
12.4日期時間類.....................................................253
█ 12.4.1Date類..................................................253
█ 12.4.2Calendar類..............................................256
12.5 大數字運算......................................................259
█ 12.5.1BigInteger類...........................................259
█ 12.5.2BigDecimal類............................................261
12.6 Runtime類.......................................................264
█ 12.6.1執行本地命令...........................................265
█ 12.6.2查看記憶體................................................266
12.7 System類........................................................267
█ 12.7.1計時....................................................268
█ 12.7.2環境屬性.................................................269
12.8 知識回顧........................................................270
第13 章集合類......................................................271
13.1集合類概述.......................................................271
13.2Collection接口...................................................271
13.3List集合.........................................................272
█ 13.3.1List接口.................................................272
█ 13.3.2List接口的實現類........................................272
█ 13.3.3Iterator疊代器..........................................274
13.4Set集合..........................................................275
█ 13.4.1Set接口..................................................275
█ 13.4.2Set接口的實現類..........................................275
13.5Map集合...........................................................277
█ 13.5.1Map接口...................................................277
█ 13.5.2Map接口的實現類...........................................278
13.6其他常用集合類...................................................279
█ 13.6.1Vector類..................................................279
█ 13.6.2Stack類...................................................281
█ 13.6.3Hashtable類...............................................282
█ 13.6.4Properties類.............................................283
13.7集合的使用場合...................................................287
13.8知識回顧.........................................................288
第14 章枚舉與泛型..............................................289
14.1枚舉.............................................................289
█ 14.1.1使用枚舉類型設定常量.....................................289
█ 14.1.2深入了解枚舉類型..........................................291
█ 14.1.3使用枚舉類型的優勢........................................297
14.2 泛型............................................................. 297
█ 14.2.1回顧“向上轉型”與“向下轉型”............................298
█ 14.2.2定義泛型類...............................................299
█ 14.2.3泛型的常規用法...........................................300
█ 14.2.4泛型的高級用法...........................................304
█ 14.2.5泛型總結.................................................307
14.3知識回顧.........................................................307
第15 章Swing程式設計......................................308
15.1Swing概述........................................................308
█ 15.1.1Swing的特點..............................................308
█ 15.1.2Swing包..................................................309
█ 15.1.3常用Swing組件概述.......................................309
15.2常用窗體.........................................................310
█ 15.2.1JFrame窗體...............................................310
█ 15.2.2JDialog對話框窗體.......................................312
15.3常用布局管理器..................................................314
█ 15.3.1絕對布局................................................314
█ 15.3.2FlowLayout流布局管理器..................................315
█ 15.3.3BorderLayout邊界布局管理器...............................317
█ 15.3.4GridLayout格線布局管理器.................................319
15.4常用面板.........................................................320
█ 15.4.1JPanel面板...............................................320
█ 15.4.2JScrollPane滾動面板......................................322
15.5 標籤組件與圖示..............................................323
█ 15.5.1JLabel標籤組件..................................323
█ 15.5.2圖示的使用.........................................324
15.6 文本組件....................................................327
█ 15.6.1JTextField文本框組件.................................327
█ 15.6.2JPasswordField密碼框組件.............................328
█ 15.6.3JTextArea文本域組件..................................329
15.7 按鈕組件......................................................330
█ 15.7.1JButton按鈕組件......................................330
█ 15.7.2JRadioButton單選按鈕組件............................332
█ 15.7.3JCheckBox複選框組件...................................335
15.8 列表組件.........................................................336
█ 15.8.1JComboBox下拉列表框組件...................................336
█ 15.8.2JList列表框組件...........................................338
15.9 常用事件監聽器...................................................340
█ 15.9.1監聽事件簡介............................................341
█ 15.9.2動作事件監聽器.........................................341
█ 15.9.3焦點事件監聽器.........................................343
15.10 知識回顧.......................................................345
第16 章I/O(輸入/輸出)....................................346
16.1流概述.............................................................346
16.2輸入/輸出流.......................................................347
█ 16.2.1輸入流.....................................................347
█ 16.2.2輸出流.....................................................349
16.3File類............................................................350
█ 16.3.1創建File對象..............................................351
█ 16.3.2檔案操作...................................................351
█ 16.3.3資料夾操作................................................354
16.4檔案輸入/輸出流....................................................356
◆16.4.1FileInputStream類與
FileOutputStream類............................................356
█ 16.4.2FileReader類與FileWriter類.................................358
16.5帶緩衝的輸入/輸出流................................................360
◆16.5.1BufferedInputStream類與
BufferedOutputStream類........................................360
◆16.5.2BufferedReader類與
BufferedWriter類................................................362
16.6數據輸入/輸出流....................................................364
16.7ZIP壓縮輸入/輸出流..................................................367
█ 16.7.1壓縮檔案...................................................367
█ 16.7.2解壓縮ZIP檔案..............................................369
16.8知識回顧............................................................371
第17 章反射.............................................................372
17.1Class類與Java反射.....................................................372
█ 17.1.1Class類.........................................................372
█ 17.1.2獲取構造方法...................................................375
█ 17.1.3獲取成員變數...................................................378
█ 17.1.4獲取方法........................................................381
17.2 Annotation註解..........................................................385
█ 17.2.1內置註解........................................................385
█ 17.2.2自定義註解......................................................385
17.3知識回顧.................................................................389
第18 章lambda表達式.................................................390
18.1lambda表達式.............................................................390
█ 18.1.1lambda表達式簡介.................................................390
█ 18.1.2lambda表達式實現函式式接口.......................................391
█ 18.1.3lambda表達式作為方法參數.........................................394
█ 18.1.4lambda表達式調用外部變數.........................................395
█ 18.1.5lambda表達式與異常處理............................................397
18.2方法的引用...............................................................399
█ 18.2.1引用靜態方法.....................................................399
█ 18.2.2引用成員方法.....................................................400
█ 18.2.3引用帶泛型的方法.................................................401
█ 18.2.4引用構造方法.....................................................402
█ 18.2.5Function接口.....................................................405
18.3集合的流式處理...........................................................406
█ 18.3.1Stream接口簡介..................................................408
█ 18.3.2Optional類.....................................................409
█ 18.3.3Collectors類......................................................411
█ 18.3.4數據過濾.........................................................412
█ 18.3.5數據映射.........................................................417
█ 18.3.6數據查找.........................................................419
█ 18.3.7數據收集.........................................................422
18.4 知識回顧................................................................427
第19 章多執行緒....................................................................428
19.1執行緒簡介....................................................................428
19.2實現執行緒的兩種方式..........................................................429
█ 19.2.1繼承Thread類...........................................................429
█ 19.2.2實現Runnable接口.......................................................431
19.3執行緒的生命周期...............................................................433
19.4操作執行緒的方法...............................................................434
█ 19.4.1執行緒的休眠.............................................................434
█ 19.4.2執行緒的加入..............................................................436
█ 19.4.3執行緒的中斷..............................................................438
19.5執行緒的優先權....................................................................440
19.6執行緒的同步.......................................................................442
█ 19.6.1執行緒安全..................................................................442
█ 19.6.2執行緒同步機制..............................................................443
19.7執行緒的暫停與恢復..................................................................445
19.8知識回顧...........................................................................448
第20 章網路通信....................................................................449
20.1網路程式設計基礎.................................................................449
█ 20.1.1區域網路與網際網路..............................................................449
█ 20.1.2網路協定......................................................................449
█ 20.1.3連線埠和套接字................................................................451
20.2IP地址封裝..........................................................................451
20.3TCP程式設計..........................................................................453
█ 20.3.1ServerSocket伺服器端..........................................................454
█ 20.3.2Socket客戶端.................................................................455
█ 20.3.3TCP網路程式實例.............................................................456
20.4UDP程式設計..........................................................................458
█ 20.4.1DatagramPacket類.............................................................459
█ 20.4.2DatagramSocket類............................................................460
█ 20.4.3UDP網路程式實例...........................................................461
20.5多執行緒聊天室.......................................................................465
20.6知識回顧...........................................................................471
第21 章使用JDBC運算元據庫……………………………………………………….472
21.1JDBC概述...........................................................................472
█ 21.1.1資料庫基礎................................................................472
█ 21.1.2JDBC簡介...................................................................473
21.2JDBC中常用的類和接口...............................................................474
█ 21.2.1DriverManager類............................................................474
█ 21.2.2Connection接口.............................................................475
█ 21.2.3Statement接口..............................................................476
█ 21.2.4PreparedStatement接口......................................................477
█ 21.2.5CallableStatement接口.......................................................478
█ 21.2.6ResultSet接口.................................................................478
21.3資料庫操作...........................................................................480
█ 21.3.1連線資料庫.................................................................480
█ 21.3.2數據查詢...................................................................481
█ 21.3.3動態查詢...................................................................484
█ 21.3.4添加、修改、刪除記錄.......................................................485
█ 21.3.5調用存儲過程..................................................................490
21.4知識回顧.............................................................................492
第22 章Swing表格組件.............................................................493
22.1利用JTable類直接創建表格............................................................493
█ 22.1.1創建表格....................................................................493
█ 22.1.2定義表格....................................................................496
█ 22.1.3操縱表格....................................................................500
22.2 表格模型與表格.......................................................................503
█ 22.2.1利用表格模型創建表格.........................................................503
█ 22.2.2維護表格模型...................................................................505
22.3提供行標題欄的表格........................................................................508
22.4知識回顧.................................................................................513
第23 章Swing樹組件.....................................................................514
23.1簡單的樹..................................................................................514
23.2處理選中節點事件...........................................................................516
23.3遍歷樹節點..................................................................................519
23.4定製樹....................................................................................524
23.5維護樹模型.................................................................................527
23.6 處理展開節點事件......................................................................531
23.7 知識回顧...............................................................................534
第24 章Swing其他高級組件…………………………………………..535
24.1高級組件面板..........................................................................535
█ 24.1.1分割面板.......................................................................535
█ 24.1.2選項卡面板.....................................................................538
█ 24.1.3桌面面板和內部窗體.............................................................541
24.2選單...................................................................................547
█ 24.2.1創建選單欄.....................................................................547
█ 24.2.2創建彈出式選單..................................................................549
█ 24.2.3定製個性化選單...................................................................551
24.3工具欄...................................................................................557
24.4檔案選擇器...............................................................................559
█ 24.4.1檔案選擇對話框...................................................................559
█ 24.4.2使用檔案過濾器..................................................................561
24.5進度條.....................................................................................562
24.6系統托盤.................................................................................565
24.7桌面集成控制項..............................................................................567
24.8知識回顧....................................................................................569
第25 章 高級布局管理器..................................................................570
25.1箱式布局管理器..........................................................................570
25.2卡片布局管理器...........................................................................573
25.3格線組布局管理器..........................................................................576
25.4彈簧布局管理器.............................................................................581
█ 25.4.1使用彈簧布局管理器...................................................................581
█ 25.4.2使用彈簧和支柱........................................................................585
█ 25.4.3利用彈簧控制組件大小.............................................................586
25.5知識回顧...................................................................................588
第26 章高級事件處理..........................................................................589
26.1鍵盤事件..................................................................................589
26.2滑鼠事件...................................................................................592
26.3窗體事件....................................................................................594
█ 26.3.1捕獲窗體焦點變化事件...............................................................595
█ 26.3.2捕獲窗體狀態變化事件................................................................596
█ 26.3.3捕獲其他窗體事件.....................................................................598
26.4選項事件......................................................................................600
26.5表格模型事件..................................................................................602
26.6知識回顧........................................................................................605
第27 章AWT繪圖..............................................................................606
27.1Java繪圖基礎...........................................................................606
█ 27.1.1Graphics繪圖類..................................................................606
█ 27.1.2Graphics2D繪圖類..............................................................606
█ 27.1.3Canvas畫布類..................................................................607
27.2繪製幾何圖形...........................................................................607
27.3設定顏色與畫筆..........................................................................611
█ 27.3.1設定顏色........................................................................611
█ 27.3.2設定畫筆.........................................................................613
27.4繪製圖片.................................................................................615
27.5圖像處理..................................................................................617
█ 27.5.1放大與縮小.......................................................................617
█ 27.5.2圖像翻轉...........................................................................619
█ 27.5.3圖像旋轉........................................................................624
█ 27.5.4圖像傾斜.........................................................................625
27.6知識回顧...................................................................................627
第28 章列印技術...................................................................................628
28.1列印控制類......................................................................................628
28.2“列印”對話框..................................................................................630
28.3列印頁面......................................................................................631
28.4多頁列印.......................................................................................634
28.5列印預覽.....................................................................................636
28.6知識回顧.......................................................................................640
第29 章JavaFX技術...........................................................................641
29.1JavaFX技術簡介................................................................................641
█ 29.1.1JavaFX包與運行環境.....................................................................641
█ 29.1.2JavaFX程式的結構.....................................................................643
█ 29.1.3Application類........................................................................646
█ 29.1.4 JavaFX程式的啟動方法...............................................................647
█ 29.1.5簡單的JavaFX實例.................................................................647
29.2文本組件.....................................................................................648
█ 29.2.1標籤................................................................................648
█ 29.2.2文本框和密碼框....................................................................649
█ 29.2.3文本域組件..........................................................................651
29.3按鈕組件...................................................................................652
█ 29.3.1Button按鈕..........................................................................652
█ 29.3.2開關按鈕...........................................................................654
█ 29.3.3單選按鈕...........................................................................655
█ 29.3.4多選按鈕.............................................................................657
29.4圖片組件...................................................................................658
29.5列表組件...................................................................................660
█ 29.5.1下拉列表............................................................................660
█ 29.5.2列表框............................................................................662
29.6選單欄組件................................................................................664
29.7工具列組件..................................................................................666
29.8畫布........................................................................................668
29.9事件.........................................................................................670
█ 29.9.1EventHanlder事件處理器接口..........................................................670
█ 29.9.2動作事件...........................................................................671
█ 29.9.3滑鼠事件...........................................................................671
█ 29.9.4鍵盤事件............................................................................673
29.10知識回顧...................................................................................675
第30 章JFreeChart圖表......................................................................676
30.1JFreeChart簡介...............................................................................676
30.2圖表的基本操作................................................................................676
█ 30.2.1繪製簡單餅圖.........................................................................676
█ 30.2.2支持中文功能..........................................................................678
█ 30.2.3設定背景..............................................................................681
█ 30.2.4設定圖例位置...........................................................................683
█ 30.2.5生成圖片檔案...........................................................................685
30.3繪製其他類型餅圖..............................................................................687
█ 30.3.13D餅圖..................................................................................687
█ 30.3.2多餅圖.................................................................................689
30.4繪製柱形圖....................................................................................692
█ 30.4.1單柱形圖..............................................................................692
█ 30.4.2多柱形圖.............................................................................695
30.5區域圖......................................................................................698
30.6氣泡圖......................................................................................701
30.7分布圖......................................................................................703
30.8折線圖........................................................................................706
30.9聯合分類圖.....................................................................................710
30.10雙軸圖........................................................................................712
30.11環形圖........................................................................................715
30.12堆積條形圖...................................................................................718
30.13時序圖........................................................................................721
30.14知識回顧.....................................................................................724