定義 java 基本資料型別
阿新 • • 發佈:2018-11-20
1 package debug; 2 3 class Demo { 4 /* 5 * 定義八種基本資料型別,如下 6 */ 7 8 public static void main(String[] args) { 9 //define char type 10 char a = '1'; //字元型別用單引號 11 System.out.println(a); 12 13 //define string type 14 String text = "Hello world"; //字串型別用雙引號 15 System.out.println(text); 16 17 //define float type 18 float f = 1.03F; //單精度浮點型別後面必須要帶上F或者f,預設用F 19 System.out.println(f); 20 21 //define double type 22 double d = 1.345; //浮點型別預設為雙精度,所以如果定義的是doule型別,後面就不需要再帶上F 23 System.out.println(d);24 25 //define short type 26 short s = 100; 27 System.out.println(s); 28 29 //define int type 30 int i = 101; 31 System.out.println(i); 32 33 //define long type 34 long l = 10000000000L; //定義長整型後面必須要帶上L或者l,預設用L 35 System.out.println(l);36 37 //define byte type 38 byte a1 = 102; 39 System.out.println(a1); 40 41 //define boolean 42 boolean b = false; 43 System.out.println(b); 44 45 46 } 47 48 }
輸出如下:
1 1 2 Hello world 3 1.03 4 1.345 5 100 6 101 7 10000000000 8 102 9 false