java语言基础--byte、short、char的混合运算

 1 /*
 2       结论:byte、char、short做混合运算的时候,各自先转换成int再做运算。
 3  */
 4 public class IntTest {
 5     public static void main(String[] args) {
 6         
 7         char c1 = 'a';
 8         byte b = 1;
 9         //注意:这里的“+”是负责求和
10         System.out.println(c1 + b);//输出结果:98
11         
12         //错误:不兼容类型:从int转换到short可能会有损失
13         //Type mismatch: cannot convert from int to short
14         //short s = c1 + b;//编译器不知道这个加法最后的结果是多少,只知道是int类型
15         //修改
16         short  s= (short)(c1 + b);
17         
18         short k = 98;//可以实现,直接赋值
19         
20         int a = 1;
21         //错误:不兼容类型:从int转换到short可能会有损失
22         //Type mismatch: cannot convert from int to short
23         short x = a;//不可以,编译器只知道a是int类型,不知道a中存储的是哪个值
24         System.out.println(x);
25     }
26 }

 

posted @ 2021-04-09 14:45  lraj  阅读(386)  评论(0编辑  收藏  举报