Java中8種基本資料型別及其預設值
阿新 • • 發佈:2018-10-31
Java語言中有8種基本資料型別,基本情況彙總如下:
Java中8種基本資料型別總結 |
|||||
序號 |
資料型別 |
大小/位 |
封裝類 |
預設值 |
可表示資料範圍 |
1 |
byte(位) |
8 |
Byte |
0 |
-128~127 |
2 |
short(短整數) |
16 |
Short |
0 |
-32768~32767 |
3 |
int(整數) |
32 |
Integer |
0 |
-2147483648~2147483647 |
4 |
long(長整數) |
64 |
Long |
0 |
-9223372036854775808~9223372036854775807 |
5 |
float(單精度) |
32 |
Float |
0.0 |
1.4E-45~3.4028235E38 |
6 |
double(雙精度) |
64 |
Double |
0.0 |
4.9E-324~1.7976931348623157E308 |
7 |
char(字元) |
16 |
Character |
空 |
0~65535 |
8 |
boolean |
8 |
Boolean |
flase |
true或false |
在Myeclipse中Java驗證程式碼如下:
[java] view plain copy print ?
- package com.test;
- abstract class Other {
- static byte a;
- static short b;
- static int c;
- static long d;
- static float e;
- static double f;
- static char g;
- static boolean h;
- //String不是基本型別
- static String str1 = "";//生成一個String型別的引用,而且分配記憶體空間來存放"";
- static String str2; //只生成一個string型別的引用;不分配記憶體空間,預設為null
- public static void main(String[] args) {
- System.out.println("byte的大小:"+Byte.SIZE+" byte的預設值:"+a+" byte的資料範圍:"+Byte.MIN_VALUE+"~"+Byte.MAX_VALUE);
- System.out.println("short的大小:"+Short.SIZE+" short的預設值:"+b+" short的資料範圍:"+Short.MIN_VALUE+"~"+Short.MAX_VALUE);
- System.out.println("int的大小:"+Integer.SIZE+" int的預設值:"+c+" int的資料範圍:"+Integer.MIN_VALUE+"~"+Integer.MAX_VALUE);
- System.out.println("long的大小:"+Long.SIZE+" long的預設值:"+d+" long的資料範圍:"+Long.MIN_VALUE+"~"+Long.MAX_VALUE);
- System.out.println("float的大小:"+Float.SIZE+" float的預設值:"+e+" float的資料範圍:"+Float.MIN_VALUE+"~"+Float.MAX_VALUE);
- System.out.println("double的大小:"+Double.SIZE+" double的預設值:"+f+" double的資料範圍:"+Double.MIN_VALUE+"~"+Double.MAX_VALUE);
- System.out.println("char的大小:"+Character.SIZE+" char的預設值:"+g+" char的資料範圍:"+Character.MIN_VALUE+"~"+Character.MAX_VALUE);
- System.out.println("boolean的大小:"+Byte.SIZE+" boolean的預設值:"+h+" boolean的資料範圍:"+Byte.MIN_VALUE+"~"+Byte.MAX_VALUE);
- System.out.println("String字串的預設值:"+str1+"str的預設長度:"+str1.length());
- System.out.println("String字串的預設值:"+str2);
- }
- }
輸出結果如下:
[java] view plain copy print ?
- byte的大小:8 byte的預設值:0 byte的資料範圍:-128~127
- short的大小:16 short的預設值:0 short的資料範圍:-32768~32767
- int的大小:32 int的預設值:0 int的資料範圍:-2147483648~2147483647
- long的大小:64 long的預設值:0 long的資料範圍:-9223372036854775808~9223372036854775807
- float的大小:32 float的預設值:0.0 float的資料範圍:1.4E-45~3.4028235E38
- double的大小:64 double的預設值:0.0 double的資料範圍:4.9E-324~1.7976931348623157E308
- char的大小:16 char的預設值:
- boolean的大小:8 boolean的預設值:false boolean的資料範圍:-128~127
- String字串的預設值:str的預設長度:0
- String字串的預設值:null