National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Introduction to Java Programming 講師 : 曾學文
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Outline Course Introduction Java Introduction Your First Cup of Java Your Turn
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering 3 Course Introduction 上課時間 ~ :00 ~ 12:00 講師:曾學文 課程網頁
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Course Introduction 課程適合對象 對於 Java 程式設計有興趣的初學者 課程目的 藉由基本 Java 程式設計概念開始,循序漸進地帶領學員 進入 Java 程式設計的殿堂。課堂上將著重於基本程式設 計觀念以及實作經驗,將藉由大量地練習範例中,讓學 員清楚了解 Java 程式設計的方法。期許學員能於課程結 束後,具有初步的程式撰寫能力,並且最重要的是具有 自行進修進階程式設計技巧。 課程進度表 依照課程情況做動態調整
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Course Introduction 參考書目 The Java Tutorial 3rd Edition Java How to Program 4th Edition ( Prentice Hall ) Thinking in Java, 3rd Edition Java 2 教學手冊 SDK 1.4 版( Beginning Java 2 SDK 1.4 Edition ),蔡 明志、陳彥仲譯,碁峰出版
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Course Introduction 成績評量 上課情況(缺曠課、課堂實例練習) 平均分數達 70 以上將給予結業證書 作業繳交情況 程式評分標準 完成程式基本項目且正確執行: 80 分 完成進階項目或加強功能:每項酌予加分 嚴禁抄襲 Questions?
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering The Origin of Java Internet arises in around Different computers run on different operating systems wish to communicate with each other more easily. A new programming language which can run across different platforms is desired. Java is developed and maintained by Sun Microsystem. The first Java platform is released at 5/23/1995. Java platforms are upgraded year by year. Java 1.0 and 1.1 is called Java 1. Java 1.2, 1.3, 1.4, and 1.5 is called Java 2.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Related Terms about Java Java application Standalone Java programs run on PC. e.g., Borland JBuilder, Intellij IDEA are writen in Java. Java Applet Java programs embedded in the Web page. The functionalities of Java Applet are restricted due to the security reasons. Java script Client-side script language running by browser. Java Server Pages (JSP) Server-side script language running on the web server.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Important Features of Java Portability Java programs are compiled into Java byte code instead of machine-dependent code. The Java byte code is interpreted by Java Virtual Machine (JVM) upon execution. Various Hardware Windows Java Applications JVM Various Hardware Linux JVM Java Applications
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Important Features of Java Garbage Collection You don’t need to address the problems of memory allocation and de-allocation. The garbage collector in JVM is responsible for managing the memory usage. Well-organized language structure The core Java library is implemented based on the object- oriented design. Some Java-specific features (e.g., interface) increase the reusability of your program.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Important Features of Java Advanced built-in data structures and classes. Vector, Hashtable, …, etc. Union and find for set operations. Object serialization. Support of multi-thread programming and exception handling Java provides synchronized block for managing multi-thread execution. The programmers are forced to handle exception (e.g., I/O error) in the Java language.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Important Features of Java 其它語言 程式開始執行後所有物件就一起載入。 一旦開始執行,就無法做出任何變更。 Java 語言 當需要某個物件時,才將該物件載入。 這種特質,讓動態抽換物件變得可能。 動態載入( Dynamic Loading )
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Important Features of Java 何謂多工 (Multi-tasking) 例如,撰寫網路程式,模擬程式。 Java 利用 “ 執行緒 ” (Thread) 來實作多工 可將一個程式分成數份, 讓各部份能同時被執行。
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Important Features of Java 安全性 Bytecode 的驗證 Java 編譯器是藉由產出與電腦架構無關的 Bytecode 指令,以達到 上述功能。 它們能輕易地在任何機器上解譯,並且動態地轉換成 原生模式的機器碼。 傳遞名稱防護 只要有一個名稱在載入時發生和別處一樣的衝突, Class Loader 將會載入本地端的版本,而拒絕載入遠端的版本 檔案存取的限制 瀏覽器不會讓 Applet 讀取不被用戶允許的文件系統 。 不使用指標 Java 與 C/C++ 之間最大的不同點之一在於 Java 不用指標器 (Pointer) , 能排除發生記憶體被蓋寫 (Overwriting Memory) 和毀損資料 (Corrupting Data) 之可能性
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Important Features of Java 安全性 在 Java 中執行一個程式需經歷層層把關,而且活動範圍 還會被侷限在特定空間。 Java 位元碼 (Byte Code) 有被竄 改過嗎? 別亂跑,我帶 你到正確的記 憶體位置。 很好!一切 正常!但只 准你在特定 範圍活動。
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Java 2 Platforms Java 2 stands for the Java programs developed by jdk1.2 or by later version. SERVER DESKTOP SMART- CARDS PDA’s & SMART- PHONES
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Java 2 Platforms Java 2 Platform Standard Edition (J2SE) Core Java Technology Provide the compiling and execution environment to build the standard Java application. Java 2 Platform Enterprise Edition (J2EE) Provide a component-based approach to the design, development, assembly, and deployment of enterprise applications (e.g., JSP, Servlet, Java Beans). Java 2 Platform Micro Edition (J2ME) The core libraries are modified to meet the restricted requirements of embedded device.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering A System Implemented in Java
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Programming Tools You can write a J2SE application with a text editor and the Java 2 Software Development Kit (SDK) SDK provides the compiling and executing environment. Latest version: J2SE 5.0 in Java 2 Runtime Environment (JRE) provides only the executing environment. Java Integrated Development Environment (IDE) Borland JBuilder Enterprise 2006 Intellij IDEA 5.0 JCreator
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Java 2 SDK Standard Edition v1.3
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering How to Learn A Programming Language as Quickly as Possible Know how to declare variables and arrays. Know how to write if-then-else. Know how to write loops. Know how many existing functions are available for this language. Know how to debug.
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Your First Cup of Java A Checklist The Java 2 SDK, Standard Edition A text editor: UltraEdit, Vim Steps Create a source file Compile the source file into a bytecode file Run the program contained in the bytecode file
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering How to write, compile and execute Java Java Program Compiler JVM Java byte code Various Hardware Windows Java Applications JVM JVM interprets the byte code into machine-dependent code xxx.java xxx.class
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Create a Source File /** * The HelloWorldApp Class implements an application that * displays “Hello World!” to the standard output. */ public class HelloWorldApp { public static void main ( String[] args ){ //Display “Hello World!” System.out.println("Hello World!"); } 程式進入點 用一般文字編輯器 存檔成 HelloWorldApp.java
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Compile the source file into a bytecode file 開始功能表 執行,打入 “cmd” 進入 Command Prompt 模式 選擇進入程式所在的資料夾
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Compile the source file into a bytecode file 編譯程式 javac HelloWorldApp.java 會產生一個叫做 HelloWorldApp.class 的 bytecode file
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Run the Program 在相同資料夾下,輸入下面指令 java HelloWorldApp
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Error Explanations 指令找不到或是 Class 找不到 【我的電腦】 【內容】 【進階】 【環境變數】 path 環境變數 : D:\j2sdk1.4.2_08\bin classpath 環境變數 :.;D:\j2sdk1.4.2_08\jre\lib\ext\abc.jar ( 外掛 Library)
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering A Closer Look at HelloWorldApp 程式註解 給程式設計師看的, compiler 會跳過註解的部分,所以註 解部分不會被執行。 Java 語言支援三種不同的程式註解方式 /* text */ 多行註解 /** documentation */ 可利用 javadoc 工具將 /** */ 之間的文字抽出,直接作成說明文 件 // 文字 單行註解
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering A Closer Look at HelloWorldApp Defining a Class 藍色粗體字部份即為主類別 (Class) 。 Java 程式至少要有一個類別,才能順利執行。 主類別名稱習慣上與檔名相同。 ( 若為 public ,則必須相同 ) /** * The HelloWorldApp class implements an application that * Displays “Hello World!” to the standard output */ public class HelloWorldApp { public static void main ( String[] args ){ System.out.println(“Hello World!”);// Print the string }
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering A Closer Look at HelloWorldApp 程式進入點 藍色粗體字部份即為進入點,為一個程式開始執行的地方。 Java 的進入點名稱為 “main()” 函數 /** * The HelloWorldApp class implements an application that * displays “Hello World!” to the standard output */ public class HelloWorldApp { public static void main ( String[] args ){ System.out.println(“Hello World!”);// Print the string }
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering A Closer Look at HelloWorldApp main(String[] args)voidstaticpublic { 程式碼寫在此處 } main: 進入點的函數名稱。一定要取名為 “main” (String[] args): 傳遞給 main 的參數。所有的宣告與名稱一定要如此寫。 void: main 的傳回值。 void 代表不傳回任何值。 static: 宣告 main 是一個靜態函數。可以讓整個 HelloWorld class 使用,而非只 有 HelloWorld 的物件可以呼叫而已。 public: 允許任何 class 呼叫這個 main method {}: 將您想執行的程式填在此處。注意 !! Java 每行程式後方都要以分號 “;” 分開。
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering A Closer Look at HelloWorldApp 在螢幕上印字 System.out.println System 類別 out 變數 println 程序 System: java 提供的一個類別,其中定義了所有與標準輸出入的相關動作。 out: System 類別中的一個變數。其型態為 PrintStream 類別 println: PrintStream 類別內的一個程序,由於 out 變數本身指向一個 PrintStream 物件,所以我們得以透過 out 呼叫 println 程序。 範例: System.out.println(“We are”+ “studying”+ “Java!!”);
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering 程式寫作習慣 程式敘述 ; 程式敘述結束符號 程式區塊 以 {} 符號包圍 習慣先括好後,再繼續往下寫 程式碼的縮排 擅用 Tab 鍵 方便找出錯誤
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering 跳脫字元 Escape Characters \’ :單引號 \” :雙引號 \\ :反斜線 \b :倒退一格 \n :換行 \t : tab 鍵 例: System.out.println(“A\tB\tC”); 例: System.out.println(“\”Java\””);
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Your Turn Practice Set up your programming environment. Write a program showing the following messages 1 st line: Your Student Number, eg. JS nd line: Your Name, eg. 王大明 3 rd line: “Hello everyone!”
National Taiwan University Department of Computer Science and Information Engineering National Taiwan University Department of Computer Science and Information Engineering Your Turn 寫一隻 Java 程式在螢幕上顯示初以下圖形: 試著使用 System.out.print() 加上跳脫字元 ***** ** ***** ** *****