基本資料型態

型態 | 關鍵字 | 位元數 | 方法類別 | 範圍 |
---|---|---|---|---|
整數 | byte | 8 | Byte | -27 ~ 27 |
整數 | short | 16 | Short | 215 ~ 215 |
整數 | int | 32 | Integer | 231 ~ 231 |
整數 | long | 64 | Long | 264 ~ 264 |
浮點數 | float | 32 | Float | 232 ~ 232 |
浮點數 | double | 64 | Double | 264 ~ 264 |
布林值 | boolean | 1 | - | - |
字元 | char | 16 | - | - |
依資料所佔位元數的不同,整數有四種,浮點數有兩種,另外布林值與字元各一種。
資料型態所佔據的位元數越多,可表示的數字範圍也越大。
浮點數的範圍等同於整數的數字長度,差別在小數點要點在哪裡。
若將boolean和char量化很奇怪,不會有人問它們的範圍值的
byte
資料型態用於儲存位元資料,例如影像、聲音等數位化串流資訊。除了I/O應用,不會使用byte。若一定要的話,byte資料型態也可以用於儲存整數數值。但他只能存-128 ~ 127..
1 2 3 4 5 6 7 8 9 10 | public class CH001 { public static void main(String agrs[]){ System.out.println( "位元 byte 的最大值:" +Byte.MAX_VALUE); System.out.println( "位元 byte 的最小值:" +Byte.MIN_VALUE); } } |
請把上面的程式碼複制到主程式的大括號中間,按下選單上的綠色氣球。
整數 short / int / long
只儲存整數數值,可細分為- 短整數 short
- 整數 int
- 長整數 long
整數的應工具和方法與型態名稱相同,僅開頭第1個英文字母大寫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | public class CH001 { public static void main(String agrs[]){ System.out.println( "位元 byte 的最大值:" +Byte.MAX_VALUE); System.out.println( "位元 byte 的最小值:" +Byte.MIN_VALUE); System.out.println( "整數 short 的範圍:" +Short.MIN_VALUE+ "~" +Short.MAX_VALUE); System.out.println( "整數 int 的範圍:" +Integer.MIN_VALUE+ "~" +Integer.MAX_VALUE); System.out.println( "整數 long 的範圍:" +Long.MIN_VALUE+ "~" +Long.MAX_VALUE); } } |
浮點數 float / double
主要用來儲存小數數值,也可以儲存整數,可細分為- 單精度浮點數 float
- 倍精度浮點數 double
浮點數的應工具和方法與型態名稱相同,僅開頭第1個英文字母大寫
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | public class CH001 { public static void main(String agrs[]){ System.out.println( "位元 byte 的最大值:" +Byte.MAX_VALUE); System.out.println( "位元 byte 的最小值:" +Byte.MIN_VALUE); System.out.println( "整數 short 的範圍:" +Short.MIN_VALUE+ "~" +Short.MAX_VALUE); System.out.println( "整數 int 的範圍:" +Integer.MIN_VALUE+ "~" +Integer.MAX_VALUE); System.out.println( "整數 long 的範圍:" +Long.MIN_VALUE+ "~" +Long.MAX_VALUE); float f= 1 .024f; double d= 1 .024d; System.out.println( "單精度 float 的範圍:" +Float.MIN_VALUE+ "~" +Float.MAX_VALUE); System.out.println( "倍精度 double的範圍:" +Double.MIN_VALUE+ "~" +Double.MAX_VALUE); } } |
不同型態的數值無法進行計算,通常需要再運算前強制轉型並放棄任一方的較精確單位。
Java語言是採用十進制的。
布林 boolean
boolean 只能表示兩種資訊,1個是 true,另1個則是 false。1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class CH001 { public static void main(String agrs[]){ System.out.println( "位元 byte 的最大值:" +Byte.MAX_VALUE); System.out.println( "位元 byte 的最小值:" +Byte.MIN_VALUE); System.out.println( "整數 short 的範圍:" +Short.MIN_VALUE+ "~" +Short.MAX_VALUE); System.out.println( "整數 int 的範圍:" +Integer.MIN_VALUE+ "~" +Integer.MAX_VALUE); System.out.println( "整數 long 的範圍:" +Long.MIN_VALUE+ "~" +Long.MAX_VALUE); float f= 1 .024f; double d= 1 .024d; System.out.println( "單精度 float 的範圍:" +Float.MIN_VALUE+ "~" +Float.MAX_VALUE); System.out.println( "倍精度 double的範圍:" +Double.MIN_VALUE+ "~" +Double.MAX_VALUE); System.out.println(Boolean.FALSE); System.out.println(Boolean.TRUE); } } |
字元 char
字元只能用來表示單一文字,可以思考為1個英文字母或1個非英文字母的任何文字。通常英文字母可以直接用鍵盤輸入,鍵盤無法表示的文字測以Unicode輸入。1 2 | System.out.println( 'K' ); System.out.println( '\u8b9a' ); |
沒有留言:
張貼留言