Java中8种基本数据类型总结
序号数据类型大小/位封装类默认值可表示数据范围
1byte(位)8Byte0-128~127
2short(短整数)16Short0-32768~32767
3int(整数)32Integer0-2147483648~2147483647
4long(长整数)64Long0-9223372036854775808~9223372036854775807
5float(单精度)32Float0.01.4E-45~3.4028235E38
6double(双精度)64Double0.04.9E-324~1.7976931348623157E308
7char(字符)16Character空0~65535
8boolean8Booleanflasetrue或false
测试代码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 + "\t byte的默认值:" + a + "\t byte的数据范围:" + Byte.MIN_VALUE + " ~ " + Byte.MAX_VALUE);
System.out.println("short的大小:" + Short.SIZE + "\t short的默认值:" + b + "\t short的数据范围:" + Short.MIN_VALUE + " ~ " + Short.MAX_VALUE);
System.out.println("int的大小:" + Integer.SIZE + "\t int的默认值:" + c + "\t int的数据范围:" + Integer.MIN_VALUE + " ~ " + Integer.MAX_VALUE);
System.out.println("long的大小:" + Long.SIZE + "\t long的默认值:" + d + "\t long的数据范围:" + Long.MIN_VALUE + " ~ " + Long.MAX_VALUE);
System.out.println("float的大小:" + Float.SIZE + "\t float的默认值:" + e + "\t float的数据范围:" + Float.MIN_VALUE + " ~ " + Float.MAX_VALUE);
System.out.println("double的大小:" + Double.SIZE + "\t double的默认值:" + f + "\t double的数据范围:" + Double.MIN_VALUE + " ~ " + Double.MAX_VALUE);
System.out.println("char的大小:" + Character.SIZE + "\t char的默认值:" + g + "\t char的数据范围:" + Character.MIN_VALUE + " ~ " + Character.MAX_VALUE);
System.out.println("boolean的大小:" + Byte.SIZE + "\t boolean的默认值:" + h + "\t boolean的数据范围:" + Byte.MIN_VALUE + " ~ " + Byte.MAX_VALUE);
System.out.println("String字符串的默认值:" + str1 + "str的默认长度:" + str1.length());
System.out.println("String字符串的默认值:" + str2);
}
结果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的默认值: char的数据范围: ~
boolean的大小:8 boolean的默认值:false boolean的数据范围:-128 ~ 127
String字符串的默认值:str的默认长度:0
String字符串的默认值:null