java条件表达式------结果类型转换问题

前天在拼题A上看到德秀出的一道题,蛮有意思的,题目如下:
在这里插入图片描述
答案:A

百度了一下,得出结论:

结论:

1. 如果第二个和第三个操作数具有相同的类型,那么它就是条件表达式的类型。
2. 如果一个操作数的类型是T,T表示 byte、short 或 char,而另一个操作数是一个int类型的常量表达式,它的值是可以用类型T表示的,那么条件表达式的类型就是T。
3. 否则,将对操作数类型运用二进制数字提升,提升至两个参数中类型最高的类型,而条件表达式的类型即提升之后的类型。

对于这个结论,小编做了以下实验(以char类型为例):

//定义变量
    static Character c1 = 'w';
    static Integer i1 = 1;

//因为小编想要看到数据的类型,所以使用包装器类型
//定义能确定数据类型的方法
    public static void judgeType(Object k){
        if(k instanceof Integer) {  System.out.println("k Integer");    }
        if(k instanceof Character) {    System.out.println("k  Character"); }
        if(k instanceof Byte) {     System.out.println("k Byte");   }
        if(k instanceof Short) {    System.out.println("k Short");  }
        if(k instanceof Float) {    System.out.println("k Float");  }
        if(k instanceof Double) {    System.out.println("k Double");  }

 public static void main(String[] args) {
        Object k;
//1
        System.out.println(k = true ? 'x' : 1.11);		//120.0
        judgeType(k);						//k Double
//2
        System.out.println(k = true ? c1 : 1);			//w
        judgeType(k);						//k Character
//3        
		System.out.println(k = true ? c1 : i1);		//119
        judgeType(k);						//k Integer
//4
        System.out.println(k = true ? c1 : i1+1);		//119
        judgeType(k);						//k Integer
//5
        System.out.println(k = true ? c1 : 1+1);		//w
        judgeType(k);						//k Character
//6
        System.out.println(k = true ? c1 : 'x'+'x');		//w
        judgeType(k);						//k Character
//7
        System.out.println(k = true ? c1 : c1 + c1);		//119
        judgeType(k);						//k Integer
//8
        System.out.println(k = true ? c1 : c1 + 'x');		//119
        judgeType(k);						//k Integer
//9
        System.out.println(k = true ? 'x' : 65535);		//x
        judgeType(k);						//k  Character
//10
        System.out.println(k = true ? c1 : 65536);		//119	
        judgeType(k);						//k Integer
//11										
        System.out.println(k = true ? c1 : -1);			//119
        judgeType(k);						//k Integer	
	}
 }
  1. 第二个操作数为char类型,第三个操作数为double类型,符合结论3,表达式类型向上转为Double类型。
  2. 第二个操作数为char类型,第三个操作数为int类型,且这个表达式的结果恰好可以由char类型表示,符合结论2,表达式类型为Character类型。
  3. 第二个操作数为char类型,第三个操作数为int类型,但i1却是变量不是常量表达式,符合结论3,表达式类型为Integer类型。
  4. 第二个操作数为char类型,第三个操作数中含有变量,不是常量表达式,符合结论3,表达式类型为Integer类型。
  5. 第二个操作数为char类型,第三个操作数为int类型常量表达式,且这个表达式的结果恰好可以由char类型表示,符合结论2,表达式类型为Character类型。
  6. 第二个操作数为char类型,第三个操作数为char字符相加的和,字符相加,转换成Ascii码计算,所以是int类型常量表达式,且这个表达式的结果恰好可以由char类型表示,符合结论2,表达式类型为Character类型。
  7. 第二个操作数为char类型,第三个操作数中含有变量,不是常量表达式,符合结论3,表达式类型为Integer类型。
  8. 第二个操作数为char类型,第三个操作数中含有变量,不是常量表达式,符合结论3,表达式类型为Integer类型。
  9. 第二个操作数为char类型,第三个操作数为int类型表达式,且这个表达式的结果恰好可以由char类型表示,符合结论2,表达式类型为Character类型。
  10. 第二个操作数为char类型,第三个操作数为int类型表达式,但这个表达式的结果不能用char类型表示,符合结论3,表达式类型为Integer类型。
  11. 第二个操作数为char类型,第三个操作数为int类型表达式,但这个表达式的结果不能用char类型表示,符合结论3,表达式类型为Integer类型。
注:char表示的范围是:0~65535

详细了解数据类型的范围请看:
https://blog.csdn.net/qq_39671159/article/details/83690690

参考:
https://blog.csdn.net/imu2008/article/details/49632835
https://blog.csdn.net/yqj2065/article/details/51633827

原创文章 42 获赞 32 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_39671159/article/details/83692161