数据类型与转换

1、常见基本数据类型

四类八种:

整数类型:byte、``shortintlong`

浮点类型:floatdouble

布尔类型:boolean

字符型:char

序号 数据类型 字节 默认值 取值范围 举例说明
1 byte(位) 1 0 -2^7 - 2^7-1 byte b = 10;
2 short(短整数) 2 0 -2^15 - 2^15-1 short s = 10;
3 int(整数) 4 0 -2^31 - 2^31-1 int i = 10;
4 long(长整数) 8 0 -2^63 - 2^63-1 long l = 10l;
5 float(单精度) 4 0.0 -2^31 - 2^31-1 float f = 10.0f;
6 double(双精度) 8 0.0 -2^63 - 2^63-1 double d = 10.0d;
7 char(字符) 2 0 - 2^16-1 char c = 'c';
8 boolean(布尔值) 1 false true、false boolean b = true;

2、数据类型转换

  • 自动转换:取值范围小的类型自动提升为取值范围大的类型。

    • byteshortchar 运算时直接提升为int
    • 转换规则:byteshortchar‐‐>int‐‐>long‐‐>float‐‐>double
  • 强制转换:数据类型 变量名 = (数据类型)被转数据值;

    自动转换是Java自动执行的,而强制转换需要我们自己手动执行。

3、ASCII码表

image-20210114172722829

posted @ 2021-01-14 18:06  ThirteenZhang  阅读(100)  评论(0)    收藏  举报