JAVA Liang Introduction to Java Programming Ninth Edition

  • Slides: 78
Download presentation
第一章 電腦、程式與JAVA Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc.

第一章 電腦、程式與JAVA Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1

輸出裝置:螢幕 螢幕(monitor)負責顯示資訊(文字及圖像)。螢幕的解析度及點距 決定了顯示的畫質。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education,

輸出裝置:螢幕 螢幕(monitor)負責顯示資訊(文字及圖像)。螢幕的解析度及點距 決定了顯示的畫質。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 8

編譯原始碼 編譯器會將整個原始碼翻譯成一份機器碼檔案, 接著這份機器碼檔案會被執行,如下圖所示。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education,

編譯原始碼 編譯器會將整個原始碼翻譯成一份機器碼檔案, 接著這份機器碼檔案會被執行,如下圖所示。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 18

Java、Web、Beyond F Java可以使用來開發網路應用程式 F Java Applets F Java 網路應用程式 F Java可用來開發執行於手機裝置 Liang, Introduction to

Java、Web、Beyond F Java可以使用來開發網路應用程式 F Java Applets F Java 網路應用程式 F Java可用來開發執行於手機裝置 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 21

Java 多功能性的例子 (Applets) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education,

Java 多功能性的例子 (Applets) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 22

PDA 與手機 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc.

PDA 與手機 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 23

Java 的歷史 F James Gosling & Sun Microsystems F Oak F 1995年 5月20日 Sun

Java 的歷史 F James Gosling & Sun Microsystems F Oak F 1995年 5月20日 Sun World F Hot. Java – 世界上首個支援Java的網路瀏覽器 F 早期歷史的網址: http: //www. java. com/en/javahistory/index. jsp Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 24

Companion Website Java 的特色 簡單 F 物件導向 F 分散式 F 直譯式 F 紮實 F

Companion Website Java 的特色 簡單 F 物件導向 F 分散式 F 直譯式 F 紮實 F 安全 F 結構中立 F 可攜 F 高度效能 F 多執行緒 F 動態的程式語言 F www. cs. armstrong. edu/liang/Java. Characteristics. pdf Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 25

Companion Website Java 的特色 簡單 F 物件導向 F 分散式 F 直譯式 F 紮實 F

Companion Website Java 的特色 簡單 F 物件導向 F 分散式 F 直譯式 F 紮實 F 安全 F 結構中立 F 可攜 F 高度效能 F 多執行緒 F 動態的程式語言 F Java實行幾種安全機制來保護你的系統 免於外來程式所造成的傷害。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 31

JDK 的版本 F JDK 1. 02 (1995) F JDK 1. 1 (1996) F JDK

JDK 的版本 F JDK 1. 02 (1995) F JDK 1. 1 (1996) F JDK 1. 2 (1998) F JDK 1. 3 (2000) F JDK 1. 4 (2002) F JDK 1. 5 (2004) 又稱 JDK 5 或 Java 5 F JDK 1. 6 (2006) 又稱 JDK 6 或 Java 6 F JDK 1. 7 (2011) 又稱 JDK 7 或 Java 7 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 37

JDK 的版本 F Java Standard Edition (J 2 SE) – 用於開發客戶端的獨立應用程式或applets F Java Enterprise

JDK 的版本 F Java Standard Edition (J 2 SE) – 用於開發客戶端的獨立應用程式或applets F Java Enterprise Edition (J 2 EE) – 用於開發伺服端的應用程式,諸如Java servlets、Java. Server Pages (JSP),以及Java. Server Faces (JSF) F Java Micro Edition (J 2 ME). – 用於開發手持裝置上的應用程式,比方說手機 本書將使用J 2 SE來介紹Java程式設計 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 38

熱門的Java開發 具 F Net. Beans F Eclipse Liang, Introduction to Java Programming, Ninth Edition,

熱門的Java開發 具 F Net. Beans F Eclipse Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 39

一支簡單的Java程式 範例程式 1. 1 //This program prints Welcome to Java! public class Welcome {

一支簡單的Java程式 範例程式 1. 1 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Welcome Run 重要注意事項:(1)要�用按鈕,你必須下載整個的幻 燈片檔案slide. zip,並將檔案解壓縮後存成文件目錄( 例如,c:幻燈片)。 (2)您必須事先下載JDK並安 奘JDK’s bin directory在你的環境路徑(例如:c: Program Filesjavajdk 1. 7. 0bin in your environment path)。 (3) 如果您使用的是Office 2010,確定Power. Point 2010. doc 和這份ppt檔案至於同一資料夾內。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 40

使用Note. Pad創建和編譯 使用Note. Pad,從DOS提示訊 息下輸入notepad Welcome. java Liang, Introduction to Java Programming, Ninth Edition,

使用Note. Pad創建和編譯 使用Note. Pad,從DOS提示訊 息下輸入notepad Welcome. java Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 41

使用Word. Pad創建和編譯 使用Word. Pad,從DOS提示訊 息下輸入write Welcome. java Liang, Introduction to Java Programming, Ninth Edition,

使用Word. Pad創建和編譯 使用Word. Pad,從DOS提示訊 息下輸入write Welcome. java Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 42

建立、編譯與執行Java程式 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All

建立、編譯與執行Java程式 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 43

編譯Java原始碼 Java語言是個高階語言,但Java bytecode卻是低階語言。bytecode 跟機器指令類似,但卻是架構中立(architecture neutral),可執行於 任何帶有Java虛擬機器(Java Virtual Machine, JVM)的平台上。 JVM並非實體的機器,而是可用來直譯Java bytecode的程式。這是 Java的主要優點之一:Java bytecode可執行於各種不同的硬體平台 與作業系統。

編譯Java原始碼 Java語言是個高階語言,但Java bytecode卻是低階語言。bytecode 跟機器指令類似,但卻是架構中立(architecture neutral),可執行於 任何帶有Java虛擬機器(Java Virtual Machine, JVM)的平台上。 JVM並非實體的機器,而是可用來直譯Java bytecode的程式。這是 Java的主要優點之一:Java bytecode可執行於各種不同的硬體平台 與作業系統。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 44

animation 追蹤程式的執行 定義main方法 (main method) //This program prints Welcome to Java! public class Welcome

animation 追蹤程式的執行 定義main方法 (main method) //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 45

animation 追蹤程式的執行 敘述執行符 (execute statement) //This program prints Welcome to Java! public class Welcome

animation 追蹤程式的執行 敘述執行符 (execute statement) //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 46

animation 追蹤程式的執行 //This program prints Welcome to Java! public class Welcome { public static

animation 追蹤程式的執行 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } 字串顯示於console上 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 47

另外兩則簡單的例子 Welcome. With. Three. Messages Compute. Expression Run Liang, Introduction to Java Programming, Ninth

另外兩則簡單的例子 Welcome. With. Three. Messages Compute. Expression Run Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 48

Companion Website 補充 F 詳見補充 I. B - 下載及安裝JDK F 詳見補充 I. C -

Companion Website 補充 F 詳見補充 I. B - 下載及安裝JDK F 詳見補充 I. C - 編譯與執行Java的細節 www. cs. armstrong. edu/liang/intro 8 e Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 49

Companion Website 於指令窗口編譯與執行Java F 設定至 JDK bin directory 的路徑 – set path=c: Program Filesjavajdk

Companion Website 於指令窗口編譯與執行Java F 設定至 JDK bin directory 的路徑 – set path=c: Program Filesjavajdk 1. 6. 0bin F 設定 classpath 將現有的 directory 納入 – set classpath=. F 編譯 – javac Welcome. java F 執行 – java Welcome Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 50

Companion Website 於Text. Pad 編譯與執行Java F 詳見補充 II. A 於網站上 Liang, Introduction to Java

Companion Website 於Text. Pad 編譯與執行Java F 詳見補充 II. A 於網站上 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 51

Companion Website 於Eclipse編譯與執行Java F 詳見補充 II. D 於網站上 Liang, Introduction to Java Programming, Ninth

Companion Website 於Eclipse編譯與執行Java F 詳見補充 II. D 於網站上 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 52

Companion Website 於Net. Beans編譯與執行Java F 詳見補充 I. D 於網站上 Liang, Introduction to Java Programming,

Companion Website 於Net. Beans編譯與執行Java F 詳見補充 I. D 於網站上 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 53

分析Java程式組成 F 類別名稱( Class name ) F main方法( Main method ) F 敘述( Statements

分析Java程式組成 F 類別名稱( Class name ) F main方法( Main method ) F 敘述( Statements ) F 敘述終止符( F 被保留字( Statement terminator ) Reserved words ) F 註解( Comments ) F 區塊( Blocks ) Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 54

類別名稱 Java程式必須帶有至少一個類別。每個類別都有 一個名字。慣例上來說,類別名稱(class name)的 頭一個字母會以大寫表示。如本例子裡,類別名 稱為Welcome。 //This program prints Welcome to Java! public class

類別名稱 Java程式必須帶有至少一個類別。每個類別都有 一個名字。慣例上來說,類別名稱(class name)的 頭一個字母會以大寫表示。如本例子裡,類別名 稱為Welcome。 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 55

main方法 第二行則定義了main方法(main method)。程式會 從main方法開始執行。一個類別可帶有數個方法。 而main方法是整個程式開始執行的進入點。 //This program prints Welcome to Java! public class Welcome

main方法 第二行則定義了main方法(main method)。程式會 從main方法開始執行。一個類別可帶有數個方法。 而main方法是整個程式開始執行的進入點。 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 56

敘述 一則敘述(statement)包含一個或多個動作。此程式的 main方法包含一個System. out. println的敘述。這個敘 述會將Welcome to Java! 這個字串顯示於console上(第 四行)。 //This program prints Welcome

敘述 一則敘述(statement)包含一個或多個動作。此程式的 main方法包含一個System. out. println的敘述。這個敘 述會將Welcome to Java! 這個字串顯示於console上(第 四行)。 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 57

敘述終止符 Java裡的各敘述都會以分號(;) 做結尾,即為敘述終止符 (statement terminator)。 //This program prints Welcome to Java! public class Welcome

敘述終止符 Java裡的各敘述都會以分號(;) 做結尾,即為敘述終止符 (statement terminator)。 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 58

被保留字(reserved words),或是關鍵字(keywords), 對編譯器來說有特別的意義,因此,在程式裡不能用 於其他用途上。比方說,當編譯器看到class這個字, 就會知道class後面的文字即是類別的名稱。 //This program prints Welcome to Java! public class Welcome

被保留字(reserved words),或是關鍵字(keywords), 對編譯器來說有特別的意義,因此,在程式裡不能用 於其他用途上。比方說,當編譯器看到class這個字, 就會知道class後面的文字即是類別的名稱。 //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 59

區塊 起始括號(brace)必定得與終止括號相稱(match), 而兩者所圍起的空間則稱為區塊(block)。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education,

區塊 起始括號(brace)必定得與終止括號相稱(match), 而兩者所圍起的空間則稱為區塊(block)。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 60

{ …} // This program prints Welcome to Java! public class Welcome { public

{ …} // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 62

( … ) // This program prints Welcome to Java! public class Welcome {

( … ) // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 63

; // This program prints Welcome to Java! public class Welcome { public static

; // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 64

// … // This program prints Welcome to Java! public class Welcome { public

// … // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 65

"…" // This program prints Welcome to Java! public class Welcome { public static

"…" // This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System. out. println("Welcome to Java!"); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 66

show. Message. Dialog 方法 JOption. Pane. show. Message. Dialog(null, "Welcome to Java!", "Display Message",

show. Message. Dialog 方法 JOption. Pane. show. Message. Dialog(null, "Welcome to Java!", "Display Message", JOption. Pane. INFORMATION_MESSAGE); Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 68

程式設計風格及註釋文件 F 適當的註解 F 命名慣例 F 適當的內縮及間距 F 區塊樣式 Liang, Introduction to Java Programming,

程式設計風格及註釋文件 F 適當的註解 F 命名慣例 F 適當的內縮及間距 F 區塊樣式 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 70

命名慣例 F 選擇有意義的命名 F 類別名稱(class name): – 類別名稱的頭一個字母會以大寫表示。 Ex: Compute. Expression. Liang, Introduction to

命名慣例 F 選擇有意義的命名 F 類別名稱(class name): – 類別名稱的頭一個字母會以大寫表示。 Ex: Compute. Expression. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 72

適當的內縮及間距 F 內縮(Indentation) – 內縮兩格 F 間距(space) – 使用空格分開程式碼 Liang, Introduction to Java Programming,

適當的內縮及間距 F 內縮(Indentation) – 內縮兩格 F 間距(space) – 使用空格分開程式碼 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 73

區塊樣式 本書使用與Java API原始碼一致的行尾格式(end-of -line style)。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013

區塊樣式 本書使用與Java API原始碼一致的行尾格式(end-of -line style)。 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 74

程式設計錯誤 F 語法錯誤(syntax errors) – 被編譯器偵測出來的錯誤 F 執行期間錯誤(runtime errors) – 導致程式不正常終止的錯誤 F 邏輯錯誤(logic errors)

程式設計錯誤 F 語法錯誤(syntax errors) – 被編譯器偵測出來的錯誤 F 執行期間錯誤(runtime errors) – 導致程式不正常終止的錯誤 F 邏輯錯誤(logic errors) – 出現在程式執行的結果與預期中的不同 Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 75

語法錯誤 public class Show. Syntax. Errors { public static main(String[] args) { System. out.

語法錯誤 public class Show. Syntax. Errors { public static main(String[] args) { System. out. println("Welcome to Java); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 76

執行期間錯誤 public class Show. Runtime. Errors { public static void main(String[] args) { System.

執行期間錯誤 public class Show. Runtime. Errors { public static void main(String[] args) { System. out. println(1 / 0); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 77

邏輯錯誤 public class Show. Logic. Errors { public static void main(String[] args) { System.

邏輯錯誤 public class Show. Logic. Errors { public static void main(String[] args) { System. out. println("Celsius 35 is Fahrenheit degree "); System. out. println((9 / 5) * 35 + 32); } } Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 78