變數、常數與資料型態 n 大綱 – 變數與常數 – 變數 – 資料型別 –資料的輸出 1
常數(Constant) n 代表程式中,某個不會變動的資料 如『1』代表數值 1,『2. 1』代表數值 2. 1 n 在程式裡,它們的值在程式執行過程中 不會改變 2
變數(Variable) 代表程式中變動的資料 n 在程式中,通常以一個名稱來代表某個 變數 n 利用 = 運算子,將常數設定給變數 n 變數 X=1; 常數 3
變數的宣告(1/2) n 變數宣告的語法如下 資料型別 變數名稱 ; 下面的敘述將宣告 A變數 int A ; 4
變數的宣告(2/2) n 若同時宣告A、B為整數變數 ,可運用『, 』符號,將變數名稱隔開 int A , B ; 5
宣告變數時,命名的限制(1/2) n 不能與Java的保留字相同,如:不能宣告 名為for的變數,因for是迴圈敘述所使用 的保留字 n 變數名稱可用英文字母、數字,以及除 了『. 』點以外的標點符號所組成 6
宣告變數時,命名的限制(2/2) n 在同一個變數的可見範圍中,變數名稱 必須是唯一的。 n 英文有大小寫的區別,如: a 變數與A變 數,將代表不同的變數 n 變數字首避免使用底線(_)與金錢符號 ($),以免被誤認為系統變數 7
變數值的設定(1/2) n 若欲使用某變數時,必須經過變數宣告 的動作,語法如下: 資料型別 變數名稱 ; 例如:宣告資料型別為整數的 A變數 int A ; 8
變數值的設定(2/2) n 設定值給變數,須運用 = 指派運算子, 語法如下 資料型別 變數名稱 = 值 ; 例如:將100指派給A變數。 int A = 100 ; 9
變數的輸出(1/2) n 運用out物件的println ( )與print ( )方法,執 行變數值的輸出,語法如下 System. out. print ( 變數名稱 ) ; System. out. println ( 變數名稱 ) ; 下面的敘述將輸出變數 i 的值 System. out. print ( i ) ; 10
變數的輸出(2/2) n 欲同時輸出變數值與字串,可用『+』運 算子,執行串連動作,如下 串連 System. out. print ( " i = " + i ) ; 字串 n 變數 i Example 11
Example: Print. VAR. JAVA public class Print. VAR { public static void main(String args[]) { int i = 1; //宣告變數並設定起始值 System. out. println(i); //輸出變數值 System. out. print("i = " + i); //用『+』運算子,串 連字串與變數 } } 12
變數的命名 變數的名稱,應盡量讓人易於瞭解變數 代表的意義,例如:用Radius、PI、Area, 分別代表圓半徑、圓周率及計算出的圓 面積。 n 匈牙利命名法則要求程式設計師,在變 數名稱前以特定字首註明該變數的資料 型別,如:若Radius變數的型別為浮點數, 則變數名稱應為d. Radius。 n Example n 13
Example: Circle. Area. JAVA public class Circle. Area { public static void main(String args[]) { double a, b, c; a = 5. 0; //設定圓半徑為 5 b = 3. 14; //設定圓周率 c = b * a; //計算圓面積 System. out. println("Area is " + c + ". "); //輸出結果 } } 14
變數的有效範圍 用”{ }”大括弧符號,表示某一段程式 敘述為一個程式區塊。 n 當變數在該程式區塊宣告時,將只能在 宣告該變數的程式區塊中被使用,此為 變數的有效範圍又稱變數的 Scope n Example n 15
Example: Block. JAVA public class Block { public static void main(String args[]) { int i = 1; //宣告變數並設定起始值 { int j = 2; //宣告變數並設定起始值 System. out. println("i = " + i + " j = " + j); //輸出變 數值 } //System. out. println("i = " + i + " j = " + j); //如果上一行被執行會因為 j 未被宣告而發生錯誤 } } 16
資料型別(1/5) - 資料型別的種類 n Java的資料型別可分為以下四類 整數 – byte、short、int與long 浮點數 – float與double 字元 – char 布林值 – boolean 17
資料型別(2/5) - 整數 n 在Java中,byte、short、int與long這四 種資料型別,用於表達整數資料,並可 表達正負數,如:-100。下表為各整數 型別的說明。 型別 記憶體空間 名稱 可表達的數值範圍 byte 8 bit 位元組 從 -128到 127 short 16 bit 短整數 從 -32, 768到 32, 767的整數 int 32 bit 整數 從 -2, 147, 483, 648到 2, 147, 483, 647的整數 long 64 bit 長整數 從 -9, 223, 372, 036, 854, 775, 808到 9, 223, 372, 036, 854, 775, 807的整數 18
資料型別(3/5) - 浮點數 n 在Java中,float與double這二種資料型別, 用於表達具有小數點的數值資料,並可 表達正負數,如:-100. 356。下表為各浮 點數型別的說明。 型別 記憶體空間 名稱 可代表數值的範圍 float 32 bit 浮點數 從 1. 4 e-45到 3. 4 e+38 double 64 bit 倍精度浮點數 從 4. 9 e-324到 1. 7 e+308 19
資料型別(4/5) - 字元 n 用於儲存字元資料的型別為char,由於 Java的char型別使用全球文字碼( Unicode)。欲設定字元變數時,字元值 前後必須以『'』符號標示,下面的敘述 將『c』字元設定給c變數。 char c = 'c'; 20
資料型別(5/5) - 布林值 n 型別為布林值的資料,其資料值僅有 true(真)與false(假)兩種,常用於程式流 程的控制。在Java中,用於儲存布林值 的型別為boolean。下面的敘述是布林值 型別變數的宣告,與變數值的設定方式。 boolean bo = false; n Example 21
Example: Data. Type. JAVA public class Data. Type { public static void main(String args[]) { byte b = 127; //宣告byte型別的變數, 並設定起始值為 127 宣告byte 型別的變數, 並設定起始值為 127 int i = 128; //宣告int型別變數, 並設定起始值為 128 宣告int 型別變數, 並設定起始值為 128 short s = 32767; //宣告short型別變數, 並設定起始值為 32767 宣告short 型別變數, 並設定起始值為 32767 long l = 342768 L; //宣告long型別變數, 並設定起始值為 342768 L 宣告long 型別變數, 並設定起始值為 342768 L float f = 1. 22 f; //宣告float型別變數, 並設定起始值為 1. 22 f 宣告float 型別變數, 並設定起始值為 1. 22 f double d = 1. 22; //宣告double型別變數, 並設定起始值為 1. 22 宣告double 型別變數, 並設定起始值為 1. 22 char c = 'c'; // 宣告char型別變數, 並設定起始值為c 宣告char 型別變數, 並設定起始值為c boolean bo = false; //宣告boolean型別變數, 並設定起始值為false 宣告boolean 型別變數, 並設定起始值為false System. out. println("b = " + b); System. out. println("i = " + i); System. out. println("s = " + s); System. out. println("l = " + l); System. out. println("f = " + f); System. out. println("d = " + d); System. out. println("c = " + c); System. out. println("bo = " + bo); } } 22
資料輸出 (1/2) - 跳脫字元 在Java中,將資料輸出到螢幕上時,將利 用System類別out物件的print ( )及println ( ) 方法。 n 在輸出資料時,可運用跳脫字元(Escape Sequence),控制資料的輸出方式 n Example n 23
Example: Printer. JAVA public class Printer { public static void main(String args[]) { int pen = 200; //宣告變數及設定起始值 System. out. println("How much does it cost ? "); //輸出字串,並換行 System. out. print("It is costed " + pen + " dollars. "); //輸出字串及變數 } } 24