1 / 37

Introduction to Java Programming

Introduction to Java Programming. 講師 : 曾學文. Outline. Course Introduction Java Introduction Your First Cup of Java Your Turn. Course Introduction. 上課時間 2008.06.30 ~ 2008.07.11 09:00 ~ 12:00 講師:曾學文 E-mail: d92005@csie.ntu.edu.tw 課程網頁 http://www.csie.ntu.edu.tw/~d92005/course.

busht
Download Presentation

Introduction to Java Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Java Programming 講師:曾學文

  2. Outline • Course Introduction • Java Introduction • Your First Cup of Java • Your Turn

  3. Course Introduction • 上課時間 • 2008.06.30 ~ 2008.07.11 • 09:00 ~ 12:00 • 講師:曾學文 • E-mail: d92005@csie.ntu.edu.tw • 課程網頁 • http://www.csie.ntu.edu.tw/~d92005/course

  4. Course Introduction • 課程適合對象 • 對於 Java 程式設計有興趣的初學者 • 課程目的 • 藉由基本 Java 程式設計概念開始,循序漸進地帶領學員進入 Java 程式設計的殿堂。課堂上將著重於基本程式設計觀念以及實作經驗,將藉由大量地練習範例中,讓學員清楚了解 Java 程式設計的方法。期許學員能於課程結束後,具有初步的程式撰寫能力,並且最重要的是具有自行進修進階程式設計技巧。 • 課程進度表 • 依照課程情況做動態調整

  5. Course Introduction • 參考書目 • The Java Tutorial 3rd Edition http://java.sun.com/docs/books/tutorial • Java How to Program 4th Edition (Prentice Hall) • Thinking in Java, 3rd Edition http://www.mindview.net/Books/TIJ • Java 2 教學手冊 SDK 1.4版(Beginning Java 2 SDK 1.4 Edition),蔡明志、陳彥仲譯,碁峰出版

  6. Course Introduction • 成績評量 • 上課情況(缺曠課、課堂實例練習) • 平均分數達 70 以上將給予結業證書 • 作業繳交情況 • 程式評分標準 • 完成程式基本項目且正確執行:80 分 • 完成進階項目或加強功能:每項酌予加分 • 嚴禁抄襲 • Questions?

  7. The Origin of Java • Internet arises in around 1995. • 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.

  8. 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.

  9. Java Applications Java Applications JVM JVM Windows Linux Various Hardware Various Hardware 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.

  10. 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.

  11. 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.

  12. 其它語言 Java 語言 Important Features of Java • 動態載入(Dynamic Loading) 當需要某個物件時,才將該物件載入。 這種特質,讓動態抽換物件變得可能。 程式開始執行後所有物件就一起載入。 一旦開始執行,就無法做出任何變更。

  13. Important Features of Java • 何謂多工 (Multi-tasking) • 例如,撰寫網路程式,模擬程式。 • Java 利用 “執行緒” (Thread) 來實作多工 可將一個程式分成數份, 讓各部份能同時被執行。

  14. Important Features of Java • 安全性 • Bytecode 的驗證 • Java編譯器是藉由產出與電腦架構無關的 Bytecode指令,以達到上述功能。 它們能輕易地在任何機器上解譯,並且動態地轉換成原生模式的機器碼。 • 傳遞名稱防護 • 只要有一個名稱在載入時發生和別處一樣的衝突,Class Loader將會載入本地端的版本,而拒絕載入遠端的版本 • 檔案存取的限制 • 瀏覽器不會讓Applet讀取不被用戶允許的文件系統 。 • 不使用指標 • Java與C/C++之間最大的不同點之一在於Java不用指標器 (Pointer),能排除發生記憶體被蓋寫 (Overwriting Memory)和毀損資料 (Corrupting Data)之可能性

  15. Java 位元碼 (Byte Code) Important Features of Java • 安全性 • 在 Java 中執行一個程式需經歷層層把關,而且活動範圍還會被侷限在特定空間。 很好!一切正常!但只准你在特定範圍活動。 別亂跑,我帶你到正確的記憶體位置。 有被竄改過嗎?

  16. SERVER DESKTOP PDA’s & SMART-PHONES SMART- CARDS Java 2 Platforms • Java 2stands for the Java programs developed by jdk1.2 or by later version.

  17. 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.

  18. A System Implemented in Java

  19. 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 http://java.sun.com/ • Java 2 Runtime Environment (JRE) provides only the executing environment. • Java Integrated Development Environment (IDE) • Borland JBuilder Enterprise 2006 • Intellij IDEA 5.0 • JCreator

  20. Java 2 SDK Standard Edition v1.3

  21. 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.

  22. 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

  23. Java Program xxx.java Java Applications Compiler JVM Windows Java byte code xxx.class Various Hardware JVM interprets the byte code into machine-dependent code JVM How to write, compile and execute Java

  24. /** * 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!"); } } 註解 程式進入點 主類別 Create a Source File • 用一般文字編輯器 • 存檔成 HelloWorldApp.java

  25. Compile the source file into a bytecode file • 開始功能表 • 執行,打入 “cmd” 進入 Command Prompt 模式 • 選擇進入程式所在的資料夾

  26. Compile the source file into a bytecode file • 編譯程式 • javac HelloWorldApp.java • 會產生一個叫做 HelloWorldApp.class的 bytecode file

  27. Run the Program • 在相同資料夾下,輸入下面指令 • java HelloWorldApp

  28. Error Explanations • 指令找不到或是 Class 找不到 • 【我的電腦】【內容】【進階】【環境變數】 • path 環境變數: D:\j2sdk1.4.2_08\bin • classpath 環境變數: .;D:\j2sdk1.4.2_08\jre\lib\ext\abc.jar (外掛 Library)

  29. A Closer Look at HelloWorldApp • 程式註解 • 給程式設計師看的,compiler會跳過註解的部分,所以註解部分不會被執行。 • Java 語言支援三種不同的程式註解方式 • /* text */ • 多行註解 • /** documentation */ • 可利用 javadoc 工具將 /** */ 之間的文字抽出,直接作成說明文件 • // 文字 • 單行註解

  30. /** * 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 } } 主類別 A Closer Look at HelloWorldApp • Defining a Class • 藍色粗體字部份即為主類別(Class)。 • Java 程式至少要有一個類別,才能順利執行。 • 主類別名稱習慣上與檔名相同。 (若為 public ,則必須相同)

  31. /** * 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 } } 進入點 A Closer Look at HelloWorldApp • 程式進入點 • 藍色粗體字部份即為進入點,為一個程式開始執行的地方。 • Java 的進入點名稱為 “main()” 函數

  32. A Closer Look at HelloWorldApp public static void main (String[] args) { 程式碼寫在此處 } main:進入點的函數名稱。一定要取名為 “main” (String[] args):傳遞給 main 的參數。所有的宣告與名稱一定要如此寫。 void: main 的傳回值。void 代表不傳回任何值。 static:宣告 main 是一個靜態函數。可以讓整個 HelloWorld class 使用,而非只有 HelloWorld 的物件可以呼叫而已。 public:允許任何 class 呼叫這個 main method {}:將您想執行的程式填在此處。注意!! Java 每行程式後方都要以分號 “;” 分開。

  33. System.out.println System 類別 out 變數 println 程序 A Closer Look at HelloWorldApp • 在螢幕上印字 System: java 提供的一個類別,其中定義了所有與標準輸出入的相關動作。 out: System 類別中的一個變數。其型態為 PrintStream 類別 println: PrintStream 類別內的一個程序,由於 out 變數本身指向一個 PrintStream 物件,所以我們得以透過 out 呼叫 println 程序。 範例:System.out.println(“We are”+ “studying”+ “Java!!”);

  34. 程式寫作習慣 • 程式敘述 • ; 程式敘述結束符號 • 程式區塊 • 以 {} 符號包圍 • 習慣先括好後,再繼續往下寫 • 程式碼的縮排 • 擅用 Tab 鍵 • 方便找出錯誤

  35. 跳脫字元 Escape Characters • \’:單引號 • \”:雙引號 • \\:反斜線 • \b:倒退一格 • \n:換行 • \t:tab 鍵 • 例:System.out.println(“A\tB\tC”); • 例:System.out.println(“\”Java\””);

  36. Your Turn • Practice • Set up your programming environment. • Write a program showing the following messages • 1st line: Your Student Number, eg. JS2001 • 2nd line: Your Name, eg. 王大明 • 3rd line: “Hello everyone!”

  37. Your Turn • 寫一隻 Java 程式在螢幕上顯示初以下圖形: • 試著使用 System.out.print() 加上跳脫字元 ***** ** ** ***** ** ** *****

More Related