基本数据类型
数值型 byte 1字节 、short 2字节、 int 4个字节、 long 8个字节
布尔型 boolean (无字节,在内存中以byte数组类型存储,1个字节,8位)
字符型 float 4个字节、 double 8 个字节
java中byte基本数据类型 1个字节
1B = 8b 所以一个byte占8位
在计算机中,数值只以补码的形式存在。
原码
反码=原码取反
补码 = 反码+1
正数原码反码补码一致
所以 byte的最大值为 0111 1111 原码反码补码一致
值为 2^7 -1 = 127
最小值:
负数可取范围为 1000 0000 - 1111 1111
1111 1111(原码) = - 127
1000 0000(原码) = -0 (因为-0 没有意义,但是又不能没有它,所以,规定 1000 0000即(-0) 为 -2^7 = -128)
基本数据类型转换 byte short char int long float double
隐式转换 小精度–>大精度
short s1 = 10;
int i1 = s1;
显式转换(强制转换) 大精度 – > 小精度 会出现精度丢失, 丢失原因: 小精度的数据类型没有能够容 纳大精度的位数,只能截取其大精度的最 后的位数。
int i2 = 10;
short s2 = (int) i2;
//基本数据类型转换
// 数值型
// 整形 byte short int long
// 浮点型 float double
// 字符型
// byte short char int long float double 小精度–>大精度
// 隐式转换 小精度–>大精度
// 强制转换 大精度 -->小精度
short s1 = 1;
// short --> int
int i1 = s1;
// int --> short
short s2 = (short) i1;
// int --> float
float f1 = 1;
// float --> int;
int i2 = (int) f1;
//精度丢失 大精度-->小精度 丢失原因:小精度的位数少,只能截取大精度的最后的数量为小精度的位数的位数。
int i3 = 129;
// 0000 1000 0001 补码: 1000 0001 反码:1000 0000 原码:0111 1111
byte b1 = (byte) i3;
System.out.println(b1);