62,620
社区成员
发帖
与我相关
我的任务
分享class Demo
{
public static void main(String[] args)
{
byte a=1;
byte b=3;
byte c=a;
c=c+b;
System.out.println(c);
}
}
/*
编译结果:
---------- JAVAC ----------
Demo.java:8: 错误: 可能损失精度
c=c+b;
^
需要: byte
找到: int
1 个错误
*/
class Demo
{
public static void main(String[] args)
{
byte a=1;
byte b=3;
byte c=a;
c+=b;
System.out.println(c);
}
}
/*
编译结果:
---------- JAVAC ----------
输出完成 (耗时 0 秒) - 正常终止
*/

class Demo1
{
public static void main(String[] args)
{
byte b=1;
/*
b=b+1的运算过程为:b+1做加法运算,其中加法运算过程是以int类型为基础的,所以最后的结果就是int。
所以在用=号赋值给b的时候,因为b为byte型,会报错,必须使用强制类型转换 b=(byte)(b+1)
*/
b=b+1;
}
}
class Demo2
{
public static void main(String[] args)
{
byte b=1;
/*
b+=1的运算过程为:
1.运算b+1,当然,也是使用int基础运算的,结果也是int
2.运算求和之后,使用强制类型转换,转换的类型就是b的类型,即byte
3.将byte类型的结果赋值给b
*/
b+=1;
}
}
另外 long tmp=365 * 24 * 60 * 60 * 1000;是错误的,因为会结果溢出的。原因就是加法运算的过程。
这样就对了 long tmp=365 * 24 * 60 * 60 * 1000l,就是参加运算的随便一个数为long型
