数据类型
byte:一个字节
char:用于定义字符类型数据。内存:1byte
short:用于定义短整型的数据。内存:2byte
int:用于定义整数类型的数据。内存:4byte
long:用于定义长整型的数据。内存:4byte
float:单精度浮点型,可以准确表示到小数点后六位。内存:4byte
double:双精度浮点型,可以准确表示到小数点后十六位。内存:8byte
数据转换
空心箭头代表数据转换可能会很有精度损失
自动转换
如上图实心箭头,从小到大转换,无精度损失
强制转换
如上图全部箭头,可能有精度损失
常见错误
结果溢出
public class leixinzhuanhuan {
public static void main(String[] args) {
//数据溢出
int a = 1000000000;//10亿
int b = 10;
int c = a * b;//溢出
long c1 = a * b;//优先为int*int结果返回int后复制给c1,结果仍然溢出
long c2 = (long) a * b;
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("a*b=" + c);
System.out.println("a*b=" + c1);
System.out.println("(long)a*b=" + c2);
}
}