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  

猜你喜欢

转载自blog.csdn.net/qq_30944053/article/details/79071563