1. 程式人生 > >Java中8種基本資料型別及其預設值

Java中8種基本資料型別及其預設值

Java語言中有8種基本資料型別,基本情況彙總如下:


Java8種基本資料型別總結

序號

資料型別

大小/

封裝類

預設值

可表示資料範圍

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

truefalse


在Myeclipse中Java驗證程式碼如下:

[java]  view plain  copy  print ?
  1. package com.test;  
  2.   
  3. abstract class Other {  
  4.       
  5.     static byte a;  
  6.     static short b;  
  7.     static int c;  
  8.     static long d;  
  9.     static float e;  
  10.     static double f;  
  11.     static char g;  
  12.     static boolean h;  
  13.       
  14.     //String不是基本型別  
  15.     static String str1 = "";//生成一個String型別的引用,而且分配記憶體空間來存放"";  
  16.     static String str2; //只生成一個string型別的引用;不分配記憶體空間,預設為null  
  17.   
  18.     public static void main(String[] args) {  
  19.         
  20.       System.out.println("byte的大小:"+Byte.SIZE+" byte的預設值:"+a+" byte的資料範圍:"+Byte.MIN_VALUE+"~"+Byte.MAX_VALUE);     
  21.       System.out.println("short的大小:"+Short.SIZE+" short的預設值:"+b+" short的資料範圍:"+Short.MIN_VALUE+"~"+Short.MAX_VALUE);     
  22.       System.out.println("int的大小:"+Integer.SIZE+" int的預設值:"+c+" int的資料範圍:"+Integer.MIN_VALUE+"~"+Integer.MAX_VALUE);     
  23.       System.out.println("long的大小:"+Long.SIZE+" long的預設值:"+d+" long的資料範圍:"+Long.MIN_VALUE+"~"+Long.MAX_VALUE);     
  24.       System.out.println("float的大小:"+Float.SIZE+" float的預設值:"+e+" float的資料範圍:"+Float.MIN_VALUE+"~"+Float.MAX_VALUE);     
  25.       System.out.println("double的大小:"+Double.SIZE+" double的預設值:"+f+" double的資料範圍:"+Double.MIN_VALUE+"~"+Double.MAX_VALUE);     
  26.       System.out.println("char的大小:"+Character.SIZE+" char的預設值:"+g+" char的資料範圍:"+Character.MIN_VALUE+"~"+Character.MAX_VALUE);     
  27.       System.out.println("boolean的大小:"+Byte.SIZE+" boolean的預設值:"+h+" boolean的資料範圍:"+Byte.MIN_VALUE+"~"+Byte.MAX_VALUE);     
  28.         
  29.       System.out.println("String字串的預設值:"+str1+"str的預設長度:"+str1.length());     
  30.       System.out.println("String字串的預設值:"+str2);     
  31.         
  32.   
  33.     }  
  34.   
  35. }  

輸出結果如下:

[java]  view plain  copy  print ?
  1. byte的大小:8 byte的預設值:0 byte的資料範圍:-128~127  
  2. short的大小:16 short的預設值:0 short的資料範圍:-32768~32767  
  3. int的大小:32 int的預設值:0 int的資料範圍:-2147483648~2147483647  
  4. long的大小:64 long的預設值:0 long的資料範圍:-9223372036854775808~9223372036854775807  
  5. float的大小:32 float的預設值:0.0 float的資料範圍:1.4E-45~3.4028235E38  
  6. double的大小:64 double的預設值:0.0 double的資料範圍:4.9E-324~1.7976931348623157E308  
  7. char的大小:16 char的預設值:  
  8. boolean的大小:8 boolean的預設值:false boolean的資料範圍:-128~127  
  9. String字串的預設值:str的預設長度:0  
  10. String字串的預設值:null