Java 八大基本数据类型简述

java 的基本数据类型关系图

Java 八大基本数据类型简述_第1张图片

1、取值范围

类型 默认值 占用存储空间/字节 范围
byte 0 1 -127~128(-2的7次方到2的7次方-1)
short 0 2 -32768~32767(-2的15次方到2的15次方-1)
int 0 4 -2147483648~2147483647(-2的31次方到2的31次方-1)
long 0 8 -9223372036854774808~9223372036854774807(-2的63次方到2的63次方-1)
float 0.0 4

3.402823e+38 ~ 1.401298e-45(e+38表示是乘以10的38次方,同样

,e-45表示乘以10的负45次方)

double 0.0 8 1.797693e+308~ 4.9000000e-324
char 2 \u0000~\uFFFF
boolean false --------------- false、true


Java 八大基本数据类型简述_第2张图片2、从小到大的相关关系图

图中从左向右的转换都是隐式转换,无需再代码中进行强制转换 

byte i = 12;
System.out.println("byte:"+i);
short i2 = i;
System.out.println("short:"+i2);
int i3 = i;
System.out.println("int:"+i3);
long i4 = i;
System.out.println("long:"+i4);
float i5 = i;
System.out.println("float:"+i5);
double i6 = i;
System.out.println("double:"+i6);


char j = '²';
System.out.println("char:"+j);
int j3 = j;
System.out.println("int:"+j3);
long j4 = j;
System.out.println("long:"+j4);
float j5 = j;
System.out.println("float:"+j5);
double j6 = j;
System.out.println("double:"+j6);

输出:

byte:12
short:12
int:12
long:12
float:12.0
double:12.0
char:²
int:178
long:178
float:178.0
double:178.0

从右向左均要进行强制类型转换,才能通过编译。强制转换会丢失精度

double i = 178.33;
System.out.println("double:"+i);
float i1 = (float) i;
System.out.println("float:"+i1);
long i2 = (long) i;
System.out.println("long:"+i2);
int i3 = (int) i;
System.out.println("int:"+i3);
short i4 = (short) i;
System.out.println("short:"+i4);
byte i5 = (byte) i;
System.out.println("byte:"+i5);
char i6 = (char) i;
System.out.println("char:"+i6);

输出:

double:178.33
float:178.33
long:178
int:178
short:178
byte:-78
char:²

你可能感兴趣的:(书山有路)