作为初学者,我想验证下强制类型转换后能否输出的问题,不在乎是否丢失精度哈,写了下面一段代码,为什么一输出就报错呢?
public class QiangZhiZhuanHuan1
{
public static void main(String[] args)
{
int a=12;
byte b=13;
byte c=(byte)a+b;
System.out.println(c);
}
}
改成下面这样才不会出错:
public class QiangZhiZhuanHuan1
{
public static void main(String[] args)
{
int a=12;
byte b=13;
byte c=(byte)(a+b);
System.out.println(c);
}
}
有点不太明白,变量b已经在前面定义为byte类型啦,什么在进行强制转换时候还需要对(a+b)的结果进行转换,我直接对a进行强制类型转换成byte类型,然后再和b相加不行吗?