java的八種基本數(shù)據(jù)類型,分別是:byte(位)、short(短整數(shù))、int(整數(shù))、long(長(zhǎng)整數(shù))、float(單精度)、double(雙精度)、char(字符)、boolean(布爾值)。
關(guān)于Java的8種基本數(shù)據(jù)類型,其名稱、位數(shù)、默認(rèn)值、取值范圍及示例如下表所示:
序號(hào) |
數(shù)據(jù)類型 |
位數(shù) |
默認(rèn)值 |
取值范圍 |
舉例說(shuō)明 |
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(長(zhǎng)整數(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; |
為了驗(yàn)證表格里的內(nèi)容,在eclipse里運(yùn)行驗(yàn)證代碼如下:
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 +";默認(rèn)值:"+b +";數(shù)據(jù)范圍:"+Byte.MIN_VALUE+" - "+Byte.MAX_VALUE); System.out.println("short的大?。?quot;+Short.SIZE +";默認(rèn)值:"+s +";數(shù)據(jù)范圍:"+Short.MIN_VALUE+" - "+Short.MAX_VALUE); System.out.println("int的大?。?quot;+Integer.SIZE +";默認(rèn)值:"+i +";數(shù)據(jù)范圍:"+Integer.MIN_VALUE+" - "+Integer.MAX_VALUE); System.out.println("long的大?。?quot;+Long.SIZE +";默認(rèn)值:"+l +";數(shù)據(jù)范圍:"+Long.MIN_VALUE+" - "+Long.MAX_VALUE); System.out.println("float的大?。?quot;+Float.SIZE +";默認(rèn)值:"+f +";數(shù)據(jù)范圍:"+Float.MIN_VALUE+" - "+Float.MAX_VALUE); System.out.println("double的大?。?quot;+Double.SIZE +";默認(rèn)值:"+d +";數(shù)據(jù)范圍:"+Double.MIN_VALUE+" - "+Double.MAX_VALUE); System.out.println("char的大小:"+Character.SIZE +";默認(rèn)值:"+c +";數(shù)據(jù)范圍:"+Character.MIN_VALUE+" - "+Character.MAX_VALUE); System.out.println("boolean的大?。?quot;+Byte.SIZE +";默認(rèn)值:"+bo +";數(shù)據(jù)范圍:"+Byte.MIN_VALUE+" - "+Byte.MAX_VALUE); } }
在控制臺(tái)輸出結(jié)果如下圖所示:
此處為什么輸出char的數(shù)據(jù)范圍不是0 – 65535呢?
Java中的char類型由兩個(gè)字節(jié)即十六位來(lái)表示,因?yàn)槭菬o(wú)符號(hào)數(shù),所以為2的16次方,數(shù)值范圍就為:0 – 2^16-1;
推薦教程:《java教程》