Top Banner
1 Use of Java JDK and Have Your First Cup of Java
39

1 Use of Java JDK and Have Your First Cup of Java.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Use of Java JDK and Have Your First Cup of Java.

1

Use of Java JDK and Have Your First Cup of Java

Page 2: 1 Use of Java JDK and Have Your First Cup of Java.

2

Reference Sites

• Your First Cup of Java– http://java.sun.com/docs/books/tutorial/getStarted/cu

pojava/index.html

• The Anatomy of an Applet– http://java.sun.com/docs/books/tutorial/getStarted/ap

plet/index.html

• Defining a Class– http://java.sun.com/docs/books/tutorial/getStarted/ap

plication/index.html

Page 3: 1 Use of Java JDK and Have Your First Cup of Java.

3

Outlines

• Java Development Kit( JDK)• Simple Program

Page 4: 1 Use of Java JDK and Have Your First Cup of Java.

4

Java Development Kit( JDK)

Page 5: 1 Use of Java JDK and Have Your First Cup of Java.

5

What is JDK ?• Java 開發工具( Java Development Kit, J

DK )是昇陽公司( Sun Mircosystems, Inc )所發展的一套 Java 程式開發軟體。

• JDK 與其參考文件( Java docs )同樣是撰寫 Java 程式的必備工具。

Page 6: 1 Use of Java JDK and Have Your First Cup of Java.

6

下載 JDK

• 進入網站 http://java.sun.com/j2se/1.4.1/

• 選擇 J2SE Download → J2SE 1.4.1 Beta → Windows (all languages, including English) → SDK Download

Page 7: 1 Use of Java JDK and Have Your First Cup of Java.

7

安裝 JDK

• 進入網站 http://java.sun.com/

• 選擇 Download J2SE v.1.4.1→Windows (all language)→SDK

Page 8: 1 Use of Java JDK and Have Your First Cup of Java.

8

修改 Autoexec.bat

• 在 Autoexec.bat 中加入 path– Java 的執行檔包含於下列目錄 c:\j2sdk1.4.1\bin– 若 Autoexec.bat 中 path 原為 SET PATH=%PATH%;c:\Oak– 改成下列敘述 , 存檔並重新開機 SET PATH=%PATH%;c:\Oak;c:\j2sdk1.4.1\bin

Page 9: 1 Use of Java JDK and Have Your First Cup of Java.

9

JDB

• Java debugging

• Text mode debugging environment

Page 10: 1 Use of Java JDK and Have Your First Cup of Java.

10

Simple Program

Page 11: 1 Use of Java JDK and Have Your First Cup of Java.

11

撰寫 App2_1.java

public class app2_1

{

public static void main(String args[]) // 主程式開始 {

int num; // 宣告整數 num

num=2; // 將 num 設值為 2

System.out.println(“I have ”+num+“ dogs”);

System.out.println("You have "+num+" dogs, too");

}

}

Page 12: 1 Use of Java JDK and Have Your First Cup of Java.

12

Java Develop Environment

Editor

Compiler

Class Loader

app1_1.java source code

Bytecode Verifier

Interpreter

app1_1.class bytecodes

Hello Java !! in screen

Page 13: 1 Use of Java JDK and Have Your First Cup of Java.

13

Compile and Interpret

• 將 Java 程式存檔在 c:\Java\app2_1.java

• 進入存放程式的目錄> cd c:\Java

• Compile java program 產生 bytecodes> javac app2_1.java

• 執行 bytecodes> java app2_1

Page 14: 1 Use of Java JDK and Have Your First Cup of Java.

14

執行過程

c:\WINDOWS > cd ..

c:\> cd Java

c:\Java> javac app2_1.java

c:\Java> java app2_1

I have 2 dogs.

You have 2 dogs, too.

c:\Java>

Page 15: 1 Use of Java JDK and Have Your First Cup of Java.

15

Comments

• 註解有助於程式的閱讀與偵錯,因此有助於提高程式的可讀性。

• Java 語言的註解是以「 // 」記號開始, 至該行結束來表示註解的文字。

• The Java supports three kinds of comments: – /* text */

– /** documentation */

– // text

Page 16: 1 Use of Java JDK and Have Your First Cup of Java.

16

類別( class )• Java 程式是由類別( class )所組成:• 你可以把 Java 的 class 當成 object,

或是 C++ 的 class.

Page 17: 1 Use of Java JDK and Have Your First Cup of Java.

17

method

• Java 的 method 類似於 C 語言裡的函數 (function) 。– public static void main(String args[])– public static int star(int n) // 傳回 integer

Page 18: 1 Use of Java JDK and Have Your First Cup of Java.

18

main() method

• 每一個 Java 程式必須有一個 main() method ,而且只能有一個。

• main() method 即是程式執行的起始點。 • main() 一定要宣告成 public ,使得在類別的其它地方可以呼叫到它。

Page 19: 1 Use of Java JDK and Have Your First Cup of Java.

19

Public v.s. Private

• Public 則是用來表示該 class ( 或參數 ) 為共有,也就是在整個程式裡都可以存取到它。– 如果將一個類別宣告成 public ,則也要將檔案名稱取成這個類別的名稱, Java 在這一點的規定上頗為特殊。

• 設成 Private 的參數則只有 class 內可存取。

Page 20: 1 Use of Java JDK and Have Your First Cup of Java.

20

變數的宣告• 一般變數的宣告

– int num=123; // 宣告 num 為整數變數

– long num=32998399887L; // 長整數型態 (long)

– double num= -6.32E16; // 倍精度浮點數型態 – float num=2.0f; // 宣告 num 為浮點數變數

Page 21: 1 Use of Java JDK and Have Your First Cup of Java.

21

資料型態( Data Structure )資料型態 位元組 表示範圍long( 長整數 ) 8 -9223372036854775808 ~ 9223372036854775807

int( 整數 ) 4 -2147483648 ~ 2147483647

short( 短整數 ) 2 -32768 ~ 32767

byte( 位元 ) 1 -128 ~ 127

char( 字元 ) 1 0 ~ 255

boolean( 布林 ) 1 布林值只能使用 true 或 false

float( 浮點數 ) 4 -3.4E38(-3.4×1038) ~ 3.4E38(3.4×1038)

double( 倍精數 ) 8 -1.7E308(-1.7×10308) ~ 1.7E308(1.7×10308)

Page 22: 1 Use of Java JDK and Have Your First Cup of Java.

22

長整數型態 (long) 的最大值•程式碼裡需要用到長整數的最大值,可用下面的語法來表示:

java.lang . Long . MAX_VALUE

類別庫 長整數類別 最大值的代碼– 如要使用某個型態的代碼,則必須先指定該型類所在的類別庫,以及該型態所屬的類別。

– Example :• long lmax2=java.lang.Long.MAX_VALUE;• long lmax1=Long.MAX_VALUE;

Page 23: 1 Use of Java JDK and Have Your First Cup of Java.

23

Unicode

• 各種 computer 有不同的編碼系統,如 ASCII 、 Big5 。所以資料跨平台時就會發生錯誤。

• Java 使用 Unicode 。• Example of ‘a’ :

– char ch1=97; \\ 10 進位值– char ch2=‘a’;– char ch3=‘\u0061’; \\ 16 進位值

Page 24: 1 Use of Java JDK and Have Your First Cup of Java.

24

Escape Character

• 跳脫字元所代表的意義為“跳脫原字元所代表的意義”:– \f 換頁 (Form feed) \\反斜線 (Backslash)– \b 倒退一格 (Backspace) \'單引號(Single quote)– \n 換行(New line) \"雙引號(Double quote)

– \r 歸位(Carriage return) \t 跳格 (Tab) – \uxxxx 以十六進位表示的 unicode 字元 xxxx– \ddd 以八進位表示的 unicode字元,範圍在八進位的000~377 之間

Page 25: 1 Use of Java JDK and Have Your First Cup of Java.

25

變數預設值• 與 C/C++ 不同的是, Java 在變數的宣告,若沒有設定初值,則會設定預設值給它。

• 下表列出了各種型態的預設值:– 資料型態 預設值– byte 0 - float 0.0f– short 0 - double 0.0d– int 0 - char \u0000– long 0L - boolean false

Page 26: 1 Use of Java JDK and Have Your First Cup of Java.

26

變數名稱 • 您可以依個人的喜好來決定變數的名稱,這些變數的名稱不能使用到 Java 的關鍵字。

• Java 變數名稱的字元可以是英文字母、數字或底線。

• Java 變數名稱中不能有空白字元,且第一個字元不能是數字。

• Java 的變數有大小寫之分。

Page 27: 1 Use of Java JDK and Have Your First Cup of Java.

27

關鍵字( keyword ) • keyword 是編譯程式本身所使用的識別字,如 int 、 void 與 static 等均屬於 Java 常用的關鍵字。

• 雖然 const 與 goto 在 Java 中並沒有任何的意義,卻是保留字 (reserved word) ,它們和關鍵字一樣,在程式裡不能用來做為自訂的識別字。

Page 28: 1 Use of Java JDK and Have Your First Cup of Java.

28

識別字( identifier ) • 在 Java 中,我們稱變數或者 method 的名稱為識別字。

Page 29: 1 Use of Java JDK and Have Your First Cup of Java.

29

println

• System.out 是指標準輸出,其後所接續的文字 println ,是由 print 與 line 所組成的,意義是將後面括號中的內容列印於標準輸出設備 -- 螢幕上。

• Example :int num=3; char ch='A'; System.out.println(num +"is an integer");System.out.println(ch +"is a character");

Page 30: 1 Use of Java JDK and Have Your First Cup of Java.

30

大括號、區段及主體 • 左大括號( { )為 class 本體( body )的開始,而整個類別的本體至右大括號( } )結束。

• 每個指令敘述結束時,必須以分號「;」做結尾。

• 當某個指令的敘述不只一行時,必須以一對大括號( { } )將這些敘述括起來,形成一個區段( segment )。– Example App2_2.java

Page 31: 1 Use of Java JDK and Have Your First Cup of Java.

31

app2_2.java

01 // app2_2,簡單的 Java 程式02 public class app2_2 // 定義 public 類別 app2_203 {04 public static void main(String args[])05 {06 int i;07 for(i=1;i<3;i++)08 {09 System.out.print(i+"*"+i); //印出變數及字串內容10 System.out.println("="+i*i);11 }12 }13 }

public 類別 app2_2 的主體

for 迴圈所屬的區段

main() method 的主體

Page 32: 1 Use of Java JDK and Have Your First Cup of Java.

32

鍵盤輸入資料 • 從鍵盤輸入的所有的文、數字, Java 皆視為字串 String ,因此程式在處理上很簡單,只要將輸入的內容設定給一個變數即可。

• Example : BufferedReader buf;

String str;

buf=new BufferedReader(new nputStreamReader(System.in));

str=buf.readLine();

Page 33: 1 Use of Java JDK and Have Your First Cup of Java.

33

app3_13 (由鍵盤輸入字串)import java.io.*; // 載入 java.io 類別庫裡所有 class

public class app3_13

{

public static void main(String args[]) throws IOException

{

BufferedReader buf;

String str;

buf=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Input a string:");

str=buf.readLine(); // 將輸入的文字指定給字串變數 str存放 System.out.println("string="+str); // 印出字串 }

}

Page 34: 1 Use of Java JDK and Have Your First Cup of Java.

34

輸入數值 • 想要由鍵盤輸入數字時,必須要經過轉換後,字串的內容才會變成數值。

• Example: (app3-14.java)int num;

String str;

BufferedReader buf;

buf=new BufferedReader(new InputStreamReader(System.in));

str=buf.readLine(); // 將輸入的文字指定給字串變數 str 存放num=Integer.parseInt(str); // 將 str 轉成 int 型態後指定給 num 存放System.out.println("The integer is "+num);

Page 35: 1 Use of Java JDK and Have Your First Cup of Java.

35

資料型態轉換的 method()

• 將 string 轉成下面 data structure :– long Long.parseLong()– int Integer.parseInt()– short Short.parseShort()– byte Byte.parseByte()– double Double.parseDouble()– float Float.parseFloat()

• 這些是 java.lang 的 classes 。

Page 36: 1 Use of Java JDK and Have Your First Cup of Java.

36

例外( Exception )• 在撰寫程式時,經常無法考慮的面面俱到,因此各種不尋常的狀況也跟著發生:– (1) 要開啟的檔案並不存在。– (2) 在存取陣列時,陣列的註標值超過了陣列容許的範圍。

– (3) 原本預期使用者由鍵盤輸入的是整數,但使用者輸入的卻是英文字母。

• Java 把這類不尋常的狀況稱為例外 (exception) 。

Page 37: 1 Use of Java JDK and Have Your First Cup of Java.

37

Java Exception Handling

• Java 有二種方法來處理 Exception :– 直接由 method 拋出例外,讓 Java 預設的例外處理機制來處理。

• 較無彈性,且通常只能印出例外訊息,接著便終止程式的執行。

• Example : app3_13.java

– 在程式碼內撰寫 try-catch 區塊來捕捉由系統拋出的例外。

• 可以靈活操控程式的流程,且做出最適當的處理。 • Example : app12_4.java

Page 38: 1 Use of Java JDK and Have Your First Cup of Java.

38

自行撰寫例外處理• 例外處理是由 try 、 catch 與 finally 三個關鍵字所組成的程式區塊,其語法如下:try{

要檢查的程式敘述 ;

...

}

catch(例外類別變數名稱 ){

例外發生時的處理敘述 ;

...

}finally{

一定會執行的程式碼 ;

}

Page 39: 1 Use of Java JDK and Have Your First Cup of Java.

39

Homework

1. Please install JDK in your PC.

2. Write a java program. Let i be the maximum value of integer. Print out the value i, i+1 and i+2. Explain the overflow.

3. Write a java program to read 2 integer and print their sum on screen.