一:基本类型
–boolean 布尔
–byte 字节
–short/
int/
long 短整数/整数/
长整数
–float/
double 浮点数
–char 字符
(一)boolean
只有true,或者false两种值
public static void main(String[] args) {
boolean a=
1>
2;
System.out.println(a);
a=
true;
//false
//a=TRUE/FALSE; 错误
}
(二)byte
byte 字节,
1 byte =
8 bits (8位)–存储有符号的,以二进制补码表示的整数–
最小值-128,最大值127–
byte 类型用在
大型数组中可以显著节约空间,主要代替小整数,因为
byte 变量占用的空间只有
int 类型的四分之一–byte在二进制文件读写中使用
public static void main(String[] args) {
byte a=
127;
System.out.println(a);
//127
a++;
//128--->越界变为-128
System.
out.println(a);
//-128
a--;
//-129越界,变为127
System.
out.println(a);
//127
}
(三)整数类型
short,16位,2个字节,有符号的以二进制补码表示的整数
–(-
32768~
32767, -
2^
15~
2^
15-
1)
int, 32位,4个字节,有符号的以二进制补码表示的整数
–(-
2147483648~
2147483647, -
2^
31~
2^
31-
1)
long, 64位,8个字节,有符号的以二进制补码表示的整数
–-
9,
223,
372,
036,
854,
775,
808(-
2^
63)~
9,
223,
372,
036,
854,
775,
807(
2^
63 -
1)
不同类型赋值会自动转换
(四)浮点类型
float,单精度,32位,4个字节,符合IEEE 754标准的浮点
数,默认值0.0f。float的范围为1.40129846432481707e-
45 ~
3.40282346638528860e+38 (无论正负)。
double,双精度,64位,8个字节,符合IEEE 754标准的浮
点数,默认值0.0d。double的范围为4.94065645841246544e-
324d ~
1.79769313486231570e+308d (无论正负) 。
float和double都不能用来表示很精确的数字。
public static void main(String[] args) {
//float f=1.23; //错误,必须在后面加上f
float f=
1.23f;
double d=
4.56d;
double e=
4.564667877777979464646;
//正确,可以省略d
System.out.println(f);
//1.23
System.
out.println((
double)f);
//1.2300000190734863小转大,精度缺失
System.
out.println((
float)e);
//4.5646677会截断
System.out.println(f==
1.22999999999f);
//true
System.
out.println(f-
1.2299999999f);
//0.0
System.out.println(d==
4.55999999999999999);
//true
System.
out.println(d-
4.55999999999999999);
//0.0
}
(五)字符类型
char是一个单一的
16 位 Unicode 字符最小值是 \u0000(即为0);最大值是 \uffff(即为65,
535);
char 数据类型可以储存任何字
c中char是一字节存储,8位
(六)转换类型
char可以赋值给long和int,但是不能赋值给short和byte(编译错误)
char可以赋值给float和double
boolean与其他数据类型间没有兼容性
转载于:https://www.cnblogs.com/ssyfj/p/10188775.html