關(guān)于我們
書單推薦
新書推薦
|
碼解Java:讓初學(xué)者讀懂代碼的入門書 讀者對象:Java初學(xué)者,以及從事Java開發(fā)工作的人員。
本書主要通過代碼案例幫助讀者學(xué)習(xí)Java基礎(chǔ)部分的相關(guān)知識,大部分內(nèi)容都是以代碼的形式呈現(xiàn)的,講解部分也融入了代碼注釋中。通過閱讀本書,讀者可以更輕松、高效地掌握J(rèn)ava語言的語法結(jié)構(gòu)與編程思維。本書首先介紹了Java語言中的基礎(chǔ)語法結(jié)構(gòu),比如常量、變量、流程控制、數(shù)組、方法(函數(shù))、面向?qū)ο、封裝、繼承、多態(tài)、抽象、接口、異常等,還介紹了日常開發(fā)中常見的一些工具類的相關(guān)使用方法。每個(gè)知識點(diǎn)都配備了相應(yīng)的實(shí)操案例,包括具體的使用場景。本書內(nèi)容以實(shí)操案例為主,對以實(shí)操為出發(fā)點(diǎn)的讀者會更加友好。筆者(老邪)認(rèn)為,可以通過搜索引擎輕松了解的知識,比如Java語言的概念、理論、歷史、定義等相關(guān)內(nèi)容,沒有必要在本書中占用過多的篇幅。并且筆者認(rèn)為,一切沒有實(shí)操結(jié)果支撐的概念、理論都過于抽象,一些專業(yè)技術(shù)名詞更加難以理解。所以在本書中,筆者整理了近百個(gè)實(shí)操案例,幫助讀者快速地上手Java。
王冰,筆名為IT老邪,T教培行業(yè)從業(yè)十余年,累計(jì)授課時(shí)長超過兩萬小時(shí),從業(yè)期間經(jīng)手學(xué)員數(shù)以萬計(jì)授課內(nèi)容涵蓋、C/C++ 語言、嵌入式開發(fā)、JavaWeb全棧開發(fā)、Web前端開發(fā)、PHP全棧開發(fā)、以及 Python 等主流開發(fā)語言。曾獲得高校畢業(yè)生就業(yè)協(xié)會頒發(fā)的雙師型導(dǎo)師認(rèn)證,也曾任多個(gè)高校的牌品專業(yè)課講師。抖音自媒體平臺中粉絲量十余萬。
目 錄
第 1 章 小肆的第一個(gè) Java 程序...........................................................1 1.1 Java 開發(fā)工具 ..................................................................................................... 1 1.1.1 編碼工具 ....................................................................................1 1.1.2 IDE 集成開發(fā)工具 ....................................................................1 1.2 環(huán)境搭建 ............................................................................................................. 2 1.2.1 JVM、JRE、JDK 介紹 .............................................................2 1.2.2 JDK 的下載與安裝 ....................................................................2 1.2.3 IntelliJ IDEA 的安裝與使用 .....................................................5 第 2 章 常量與變量.......................................................................... 11 2.1 常量 ................................................................................................................... 11 2.2 數(shù)據(jù)類型的分類 ............................................................................................... 12 2.2.1 基本數(shù)據(jù)類型 ..........................................................................13 2.2.2 引用數(shù)據(jù)類型 ..........................................................................13 2.3 變量 ................................................................................................................... 14 2.3.1 變量的定義 ..............................................................................14 2.3.2 變量的輸出 ..............................................................................15 2.3.3 使用變量的注意事項(xiàng) ..............................................................17 X | 碼解Java:讓初學(xué)者讀懂代碼的入門書 2.3.4 數(shù)據(jù)類型轉(zhuǎn)換 ..........................................................................17 2.4 本章思考 ........................................................................................................... 19 第 3 章 運(yùn)算符 ................................................................................ 21 3.1 運(yùn)算符的分類 ................................................................................................... 21 3.1.1 算術(shù)運(yùn)算符 ..............................................................................21 3.1.2 邏輯運(yùn)算符 ..............................................................................22 3.1.3 關(guān)系運(yùn)算符 ..............................................................................23 3.1.4 三元運(yùn)算符 ..............................................................................24 3.1.5 位運(yùn)算符(了解) ..................................................................24 3.1.6 賦值運(yùn)算符 ..............................................................................26 3.1.7 自增 / 自減運(yùn)算符 ...................................................................27 3.2 思考 ................................................................................................................... 28 3.3 運(yùn)算符優(yōu)先級 ................................................................................................... 28 第 4 章 流程控制之判斷結(jié)構(gòu)............................................................. 29 4.1 程序運(yùn)行流程(順序) ................................................................................... 29 4.2 程序運(yùn)行流程(可控) ................................................................................... 29 4.3 if 判斷 ................................................................................................................ 30 4.3.1 if 的單獨(dú)使用 ...........................................................................30 4.3.2 if 與 else 的配合使用 ..............................................................32 4.3.3 else if 的使用 ...........................................................................33 4.4.4 if 的嵌套使用 ...........................................................................38 4.4 思考 ................................................................................................................... 38 4.4.1 判斷奇數(shù) / 偶數(shù) .......................................................................39 4.4.2 判斷平閏年 ..............................................................................39 第 5 章 流程控制之分支 ................................................................... 40 5.1 標(biāo)準(zhǔn)語法 ........................................................................................................... 40 5.2 語法規(guī)則 ........................................................................................................... 41 5.3 案例 ................................................................................................................... 42 目 錄 | XI 5.3.1 成績等級劃分 ..........................................................................42 5.3.2 使用鍵盤進(jìn)行輸入 ..................................................................43 5.3.3 計(jì)算器的基本功能 ..................................................................44 5.3.4 用戶輸入一個(gè)年份和月份,輸出其對應(yīng)的天數(shù) ..................45 5.3.5 愷撒日期 ..................................................................................46 第 6 章 流程控制之循環(huán) ................................................................... 49 6.1 循環(huán)的種類及標(biāo)準(zhǔn)寫法 ................................................................................... 49 6.1.1 while 循環(huán) ................................................................................49 6.1.2 do while 循環(huán) ...........................................................................50 6.1.3 for 循環(huán) ....................................................................................51 6.2 break 和 continue ............................................................................................... 52 6.2.1 break 的用法 ............................................................................52 6.2.2 continue 的用法 .......................................................................54 6.3 案例 ................................................................................................................... 55 6.3.1 輸出 1-10 ..................................................................................55 6.3.2 輸出 1 ~ 100 累加和 ..............................................................55 6.3.3 輸出 1 ~ 100 的偶數(shù)和 ..........................................................56 6.3.4 輸出所有水仙花數(shù) ..................................................................56 6.3.5 輸出 9 行 9 列星號組成的矩形 ..............................................57 6.3.6 輸出 9 行星號組成的直角三角形 ..........................................57 6.3.7 九九乘法表 ..............................................................................58 6.3.8 輸出 7 行 * 號組成的等腰三角形 ..........................................59 6.3.9 輸出 1000 以內(nèi)的素?cái)?shù) ............................................................60 第 7 章 數(shù)組 ................................................................................... 61 7.1 數(shù)組的概念 ....................................................................................................... 61 7.2 數(shù)組的使用 ....................................................................................................... 61 7.2.1 數(shù)組的定義 ..............................................................................61 7.2.2 數(shù)組的初始化 ..........................................................................62 7.2.3 數(shù)組成員的訪問 ......................................................................62 XII | 碼解Java:讓初學(xué)者讀懂代碼的入門書 7.2.4 數(shù)組的特性 ..............................................................................62 7.3 案例 ................................................................................................................... 63 7.3.1 數(shù)組的遍歷 ..............................................................................63 7.3.2 求最值 ......................................................................................63 7.3.3 查找數(shù)組中指定元素的所在位置 ..........................................64 7.3.4 數(shù)組逆序存儲 ..........................................................................65 7.3.5 向有序數(shù)組中插入元素后保證數(shù)組仍有序 ..........................66 7.3.6 數(shù)組元素排序(升序、降序)——冒泡法 ..........................68 7.3.7 二維數(shù)組——矩陣轉(zhuǎn)置(了解即可) ..................................70 7.3.8 二維數(shù)組——楊輝三角(了解即可) ..................................72 第 8 章 方法(函數(shù))....................................................................... 74 8.1 方法的概念 ....................................................................................................... 74 8.1.1 方法的用途 ..............................................................................74 8.1.2 名詞解析 ..................................................................................74 8.1.3 使用方法時(shí)的注意事項(xiàng) ..........................................................75 8.1.4 方法的定義 ..............................................................................75 8.1.5 方法示例 ..................................................................................75 8.2 案例 ................................................................................................................... 78 8.3 方法的重載 ....................................................................................................... 80 8.3.1 方法的重載的特點(diǎn) ..................................................................80 8.3.2 案例 ..........................................................................................81 第 9 章 面向?qū)ο?............................................................................ 82 9.1 面向?qū)ο笾蓄惻c對象的概念 ........................................................................... 82 9.2 類的定義 ........................................................................................................... 83 9.3 對象的使用 ....................................................................................................... 83 9.3.1 對象的使用步驟 ......................................................................83 9.3.2 成員變量與局部變量 ..............................................................85 9.3.3 成員方法與普通方法 ..............................................................85 9.3.4 對象間的賦值 ..........................................................................86 目 錄 | XIII 9.3.5 方法中對象的使用 ..................................................................86 9.3.6 this 的用法 ...............................................................................87 9.4 封裝 ................................................................................................................... 87 9.5 構(gòu)造方法 ........................................................................................................... 89 9.6 案例 ................................................................................................................... 90 第 10 章 繼承.................................................................................. 94 10.1 繼承的作用(意義) ..................................................................................... 94 10.2 繼承的特點(diǎn) ..................................................................................................... 94 10.3 繼承的格式 ..................................................................................................... 95 10.4 繼承間成員的訪問 ......................................................................................... 95 10.4.1 父類、子類、局部變量名重復(fù)、沖突時(shí)訪問規(guī)則 ............95 10.4.2 方法名重復(fù)時(shí)的訪問規(guī)則——方法的重寫 ........................96 10.5 繼承中的權(quán)限 ................................................................................................. 98 第 11 章 抽象和接口 ........................................................................ 99 11.1 抽象(abstract) ............................................................................................. 99 11.1.1 abstract 修飾符 .......................................................................99 11.1.2 舉例說明 ................................................................................99 11.1.3 案例 ......................................................................................100 11.2 接口(interface) .......................................................................................... 101 11.2.1 接口的定義 ..........................................................................102 11.2.2 接口中的成員 ......................................................................102 11.3 接口的實(shí)現(xiàn) .................................................................................................... 103 11.3.1 實(shí)現(xiàn)接口的格式 ..................................................................103 11.3.2 一個(gè)類可以同時(shí)實(shí)現(xiàn)多個(gè)接口 ..........................................103 11.3.3 接口的多繼承 ......................................................................104 11.3.4 一個(gè)類可以在繼承其他類的同時(shí)實(shí)現(xiàn)一個(gè)或多個(gè)接口 ...104 11.4 final 的使用.................................................................................................... 104 11.5 案例 ................................................................................................................ 105 11.5.1 Demo01 ................................................................................105 XIV | 碼解Java:讓初學(xué)者讀懂代碼的入門書 11.5.2 Demo02 ................................................................................109 11.5.3 Demo03 ................................................................................ 110 第 12 章 多態(tài)................................................................................ 113 12.1 格式 ............................................................................................................... 113 12.2 多態(tài)調(diào)用成員方法 ....................................................................................... 113 12.3 多態(tài)調(diào)用成員屬性 ....................................................................................... 114 12.4 對象的上下轉(zhuǎn)型 ........................................................................................... 114 12.5 案例 ............................................................................................................... 114 12.5.1 動(dòng)物案例 .............................................................................. 114 12.5.2 USB 設(shè)備實(shí)例 ..................................................................... 117 第 13 章 內(nèi)部類............................................................................. 120 13.1 內(nèi)部類的概述 ............................................................................................... 120 13.1.1 內(nèi)部類的定義格式 ..............................................................120 13.1.2 內(nèi)部類的訪問特點(diǎn) ..............................................................120 13.2 內(nèi)部類的分類 ............................................................................................... 121 13.2.1 成員內(nèi)部類 ..........................................................................121 13.2.2 局部內(nèi)部類 ..........................................................................122 13.2.3 匿名內(nèi)部類 ..........................................................................123 第 14 章 常用類............................................................................. 127 14.1 包裝類 ........................................................................................................... 127 14.2 String .............................................................................................................. 130 14.2.1 字符串類的特點(diǎn) ..................................................................130 14.2.2 常用構(gòu)造方法 ......................................................................130 14.2.3 String 對象的特點(diǎn) ...............................................................131 14.2.4 String 的比較 .......................................................................131 14.2.5 常用方法 ..............................................................................132 14.2.6 示例 ......................................................................................133 14.3 StringBuilder & StringBuffer ........................................................................ 137 目 錄 | XV 14.4 String 和 StringBuilder 的相互轉(zhuǎn)換 ............................................................. 139 14.5 Arrays ............................................................................................................. 139 14.6 Math ............................................................................................................... 141 14.7 Object ............................................................................................................. 142 14.8 System ............................................................................................................ 146 14.9 Date ................................................................................................................ 147 14.10 SimpleDateFormat ....................................................................................... 148 14.11 Calendar ....................................................................................................... 150 14.11.1 獲取對象 ............................................................................150 14.11.2 常用方法 ............................................................................150 第 15 章 File ................................................................................. 152 15.1 構(gòu)造方法 ....................................................................................................... 152 15.2 File 類的創(chuàng)建功能 ........................................................................................ 153 15.3 File 類的判斷和獲取功能 ............................................................................ 153 15.4 File 類的刪除功能 ........................................................................................ 154 15.5 遞歸遍歷目錄 ............................................................................................... 155 第 16 章 I/O 流............................................................................. 157 16.1 I/O 流的分類 ................................................................................................. 157 16.2 字節(jié)流 ........................................................................................................... 158 16.2.1 字節(jié)流寫數(shù)據(jù) ......................................................................158 16.2.2 字節(jié)流輸出異常處理 ..........................................................160 16.2.3 字節(jié)流讀數(shù)據(jù) ......................................................................160 16.2.4 字節(jié)輸入 / 輸出流緩沖區(qū) ...................................................163 16.3 字符流 ........................................................................................................... 166 16.3.1 字符流寫數(shù)據(jù) —— write() .................................................168 16.3.2 字符流讀數(shù)據(jù)—— read() ...................................................169 16.3.3 復(fù)制文件 ..............................................................................170 16.3.4 緩沖區(qū)(高效讀寫) ..........................................................171 16.4 標(biāo)準(zhǔn)輸入流和標(biāo)準(zhǔn)輸出流 ........................................................................... 173 XVI | 碼解Java:讓初學(xué)者讀懂代碼的入門書 第 17 章 序列化............................................................................. 174 17.1 對象序列化 ................................................................................................... 174 17.2 對象序列化流 ............................................................................................... 174 17.3 對象反序列化流 ........................................................................................... 175 17.4 案例(讀寫) ............................................................................................... 175 17.5 序列化過程中的問題 ................................................................................... 177 第 18 章 異常................................................................................ 178 18.1 異常處理方式 ............................................................................................... 178 18.2 異常類 ........................................................................................................... 178 18.3 異常的解決方案 ........................................................................................... 179 18.4 異常的拋出 ................................................................................................... 180 18.5 自定義異常類 ............................................................................................... 181 第 19 章 Java 中的集合................................................................... 184 19.1 集合概述 ....................................................................................................... 184 19.2 ArrayList ........................................................................................................ 184 19.2.1 ArrayList 構(gòu)造方法和添加方法 .........................................185 19.2.2 ArrayList 集合中的常用方法 .............................................185 19.3 Collection ....................................................................................................... 201 19.3.1 Collection 集合概述 ............................................................201 19.3.2 Collection 集合常用方法 ....................................................202 19.3.3 Collection 集合的遍歷 ........................................................202 19.4 List ................................................................................................................. 203 19.4.1 List 集合概述 .......................................................................203 19.4.2 List 集合特有方法 ...............................................................204 19.4.3 List 集合的遍歷 ...................................................................204 19.4.4 并發(fā)修改異常 ......................................................................205 19.4.5 ListIterator ............................................................................206 19.4.6 數(shù)據(jù)結(jié)構(gòu) ..............................................................................208 19.4.7 List 集合子類 .......................................................................212 目 錄 | XVII 19.4.8 LinkedList ............................................................................213 19.5 Set .................................................................................................................. 214 19.5.1 Set 集合概述 ........................................................................214 19.5.2 Hash 值 .................................................................................215 19.5.3 Hash 表 .................................................................................217 19.5.4 HashSet .................................................................................217 19.5.5 LinkedHashSet .....................................................................218 19.5.6 TreeSet ..................................................................................219 19.6 泛型 ............................................................................................................... 224 19.6.1 泛型定義格式 ......................................................................224 19.6.2 泛型的優(yōu)點(diǎn) ..........................................................................224 19.6.3 泛型類 ..................................................................................225 19.6.4 泛型方法 ..............................................................................227 19.6.5 泛型接口 ..............................................................................229 19.6.6 泛型通配符 ..........................................................................230 19.6.7 可變參數(shù) ..............................................................................231 19.6.8 可變參數(shù)的使用 ..................................................................232 19.7 Map ................................................................................................................ 233 19.7.1 Map 集合概述和使用 ..........................................................233 19.7.2 Map 集合的常用方法 ..........................................................234 19.7.3 Map 集合的獲取功能 ..........................................................236 19.7.4 Map 集合的遍歷 ..................................................................237 19.7.5 集合嵌套 ..............................................................................239 19.7.6 Map 集合案例 ......................................................................240 19.7.7 Collections ............................................................................241 19.8 Properties ....................................................................................................... 244 第 20 章 Java 中的反射................................................................... 247 20.1 類加載器(了解) ....................................................................................... 247 20.1.1 類加載 ..................................................................................247 20.1.2 類加載器 ..............................................................................248 20.2 反射 ............................................................................................................... 249 XVIII | 碼解Java:讓初學(xué)者讀懂代碼的入門書 20.2.1 概述 ......................................................................................249 20.2.2 獲取 Class 類的對象 ...........................................................250 20.2.3 通過反射獲取構(gòu)造方法 ......................................................250 20.2.4 通過反射創(chuàng)建對象 ..............................................................251 20.2.5 反射獲取成員變量 ..............................................................252 20.2.6 反射獲取成員方法 ..............................................................253 20.2.7 反射抑制泛型檢查 ..............................................................254 20.2.8 通過配置文件實(shí)現(xiàn)反射 ......................................................255 第 21 章 Java 中的多線程................................................................ 258 21.1 實(shí)現(xiàn)多線程 ................................................................................................... 258 21.1.1 多線程的實(shí)現(xiàn):方式一 ......................................................258 21.1.2 設(shè)置和獲取線程名稱 ..........................................................259 21.1.3 線程調(diào)度 ..............................................................................260 21.1.4 線程控制 ..............................................................................261 21.1.5 線程生命周期 ......................................................................265 21.1.6 多線程的實(shí)現(xiàn):方式二 ......................................................265 21.2 線程同步 ....................................................................................................... 267 21.2.1 線程同步概述 ......................................................................267 21.2.2 線程同步的優(yōu)勢與弊端 ......................................................270 21.2.3 線程同步方法 ......................................................................270 21.2.4 線程安全的類 ......................................................................272 21.2.5 Lock ......................................................................................273 21.3 生產(chǎn)者、消費(fèi)者模型 ................................................................................... 274 21.4 Timer 定時(shí)器 ................................................................................................. 277 第 22 章 Lambda 表達(dá)式 ................................................................ 279 22.1 Lambda 概述 ................................................................................................. 279 22.1.1 簡單案例:通過 Lambda 創(chuàng)建并啟動(dòng)線程 .......................279 22.1.2 Lambda 表達(dá)式的基本格式 ................................................280 22.1.3 Lambda 表達(dá)式案例 ............................................................280 目 錄 | XIX 22.1.4 Lambda 的省略模式 ............................................................281 22.1.5 Lambda 的注意事項(xiàng) ............................................................284 22.1.6 Lambda 與匿名內(nèi)部類 ........................................................284 22.2 Lambda 支持的方法引用 ............................................................................. 284 22.1.1 常見的引用方式 ..................................................................284 22.1.2 案例 ......................................................................................284 22.3 函數(shù)式接口 ................................................................................................... 290 22.3.1 函數(shù)式接口作為方法的參數(shù) ..............................................291 22.3.2 函數(shù)式接口作為方法的返回值 ..........................................291 22.4 常用的函數(shù)式接口 ....................................................................................... 292 22.4.1 Supplier ................................................................................292 22.4.2 Consumer ..............................................................................294 22.4.3 Predicate ...............................................................................295 22.4.4 Function ................................................................................297 22.5 Stream 流 ....................................................................................................... 298 22.5.1 流的不同狀態(tài) ......................................................................298 22.5.2 Stream 流的生成方式 ..........................................................299 22.5.3 Stream 中間流的操作方式 ..................................................300 22.5.4 Stream 結(jié)束流的操作方式 ..................................................303 22.5.5 案例 ......................................................................................304 第 23 章 網(wǎng)絡(luò)編程 ......................................................................... 306 23.1 網(wǎng)絡(luò)編程概述 ............................................................................................... 306 23.2 UDP 通信程序 .............................................................................................. 308 23.2.1 UDP 通信原理 .....................................................................308 23.2.2 使用 UDP 發(fā)送數(shù)據(jù) ............................................................309 23.2.3 使用 UDP 接收數(shù)據(jù) ............................................................310 23.2.4 案例:使用 UDP 收 / 發(fā)數(shù)據(jù) ............................................. 311 23.2.5 模擬聊天室 ..........................................................................313 23.3 TCP 通信程序 ............................................................................................... 319 23.3.1 TCP 通信原理 ......................................................................319 23.3.2 使用 TCP 發(fā)送數(shù)據(jù) .............................................................319 XX | 碼解Java:讓初學(xué)者讀懂代碼的入門書 23.3.3 使用 TCP 接收數(shù)據(jù) .............................................................320 23.3.4 案例 ......................................................................................320 第 24 章 Java 中的枚舉類型 ............................................................ 329 24.1 枚舉類型的概述 ........................................................................................... 329 24.2 語法結(jié)構(gòu) ....................................................................................................... 329 24.2.1 基本語法結(jié)構(gòu)示例 ..............................................................329 24.2.2 完整語法結(jié)構(gòu)示例 ..............................................................330 24.2.3 枚舉的 set 方法與 get 方法 .................................................332 24.2.4 枚舉的常用方法 ..................................................................333 24.3 應(yīng)用場景 ....................................................................................................... 333 24.4 注意事項(xiàng) ....................................................................................................... 335
你還可能感興趣
我要評論
|