java的八種基本數(shù)據(jù)類型,分別是:byte(位)、short(短整數(shù))、int(整數(shù))、long(長整數(shù))、float(單精度)、double(雙精度)、char(字符)、boolean(布爾值)。

關(guān)于Java的8種基本數(shù)據(jù)類型,其名稱、位數(shù)、默認值、取值范圍及示例如下表所示:
序號 |
數(shù)據(jù)類型 |
位數(shù) |
默認值 |
取值范圍 |
舉例說明 |
| 1 | byte(位) | 8 | 0 | -2^7 – 2^7-1 | byte b = 10; |
| 2 | short(短整數(shù)) | 16 | 0 | -2^15 – 2^15-1 | short s = 10; |
| 3 | int(整數(shù)) | 32 | 0 | -2^31 – 2^31-1 | int i = 10; |
| 4 | long(長整數(shù)) | 64 | 0 | -2^63 – 2^63-1 | long l = 10l; |
| 5 | float(單精度) | 32 | 0.0 | -2^31 – 2^31-1 | float f = 10.0f; |
| 6 | double(雙精度) | 64 | 0.0 | -2^63 – 2^63-1 | double d = 10.0d; |
| 7 | char(字符) | 16 | 空 | 0 – 2^16-1 | char c = 'c'; |
| 8 | boolean(布爾值) | 8 | false | true、false | boolean b = true; |
為了驗證表格里的內(nèi)容,在eclipse里運行驗證代碼如下:
package com.ce.test; class Test { static byte b; static short s; static int i; static long l; static float f; static double d; static char c; static boolean bo; public static void main(String[] args) { System.out.println("byte的大小:"+Byte.SIZE +";默認值:"+b +";數(shù)據(jù)范圍:"+Byte.MIN_VALUE+" - "+Byte.MAX_VALUE); System.out.println("short的大小:"+Short.SIZE +";默認值:"+s +";數(shù)據(jù)范圍:"+Short.MIN_VALUE+" - "+Short.MAX_VALUE); System.out.println("int的大小:"+Integer.SIZE +";默認值:"+i +";數(shù)據(jù)范圍:"+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE); System.out.println("long的大小:"+Long.SIZE +";默認值:"+l +";數(shù)據(jù)范圍:"+Long.MIN_VALUE+" - "+Long.MAX_VALUE); System.out.println("float的大小:"+Float.SIZE +";默認值:"+f +";數(shù)據(jù)范圍:"+Float.MIN_VALUE+" - "+Float.MAX_VALUE); System.out.println("double的大小:"+Double.SIZE +";默認值:"+d +";數(shù)據(jù)范圍:"+Double.MIN_VALUE+" - "+Double.MAX_VALUE); System.out.println("char的大小:"+Character.SIZE +";默認值:"+c +";數(shù)據(jù)范圍:"+Character.MIN_VALUE+" - "+Character.MAX_VALUE); System.out.println("boolean的大小:"+Byte.SIZE +";默認值:"+bo +";數(shù)據(jù)范圍:"+Byte.MIN_VALUE+" - "+Byte.MAX_VALUE); } }
在控制臺輸出結(jié)果如下圖所示:

此處為什么輸出char的數(shù)據(jù)范圍不是0 – 65535呢?
Java中的char類型由兩個字節(jié)即十六位來表示,因為是無符號數(shù),所以為2的16次方,數(shù)值范圍就為:0 – 2^16-1;
推薦教程:《java教程》
站長資訊網(wǎng)