JAVA中一共有八种基本的数据类型,分别是“byte”“boolean”“char”“short”“int”“float”“double”“long”
其大小分别为1,1,2,2,4,4,8,8
其中boolean较为特殊,一般是1,也有2,4等大小,定义较为不明确
他们的定义输出分别为
byte,由于byte最大只有127,因此赋给超出他最大值的值会溢值,变成负值,当为其赋值时,也需要使用强制类型转换来赋值
byte b = (byte)128;
System.out.println(b);
boolean,boolean较为特殊只有true和false两种状态。因此赋值也只能赋给他true或者false。
boolean bo = true;
System.out.println(bo);
char,值得注意的是,如果给char赋值中文,则需要在编译时给后面加上-encoding utf-8
char c = '星';
System.out.println(c);
short,int,long,这三个都属于整型,区别只在于占用内存和大小,在面对大于int的类型时需要给后面加上特殊标识符
short s = 119;
int num = 1008611;
long l = 11421L;
System.out.println(s);
System.out.println(num);
System.out.println(l);
float,double,这两个属于浮点型,区别也只有大小和占用内存,但一般默认为double,因此在使用float时,需要给他加上标识符
float f = 3.141414F;
double d = 3.1415926;
System.out.println(d);
System.out.println(f);