Implicit conversion Java expression result of conditions

 

public class IN0102 {
    public static void main(String[] args) {
        int a = 'a';
        System.out.println(a);//97
        float f = (float) 3.2;
        
        float sum =1.5f;
        int num=2;
        System.out.println((sum<2?1:num/sum));//1.0
        System.out.println("------------------------------------------");
        
        int x = 6;
        double d = 7.7;
        System.out.println((x>d) ? 8.0:9);//9.0
        System.out.println("------------------------------------------");
        
        System.out.println(false ? 3.0 : 1);//1.0        
        System.out.println( false ? 1 : 'a');//a
        System.out.println( false ? 1.0 : 'a');//97.0        
        System.out.println( true ? 98 : 'a');//b
        System.out.println( true ? 98.0 : 'a');//98.0
    }
}

JAVA conditional expressions, note the following:

 1, if both sides of the colon, is the same type, then the value of the conditional expression is of the same type.

2, if one type side is byte, short or char (temporarily referred to as X type), and the other side is an int constant (note is a constant), and the constant can be represented by X type (i.e., does not overflow), then, The result is X type. such as:

System.out.println (true 98: 'a'?); Output: b

3, if the situation does not meet the above, the automatic implicit conversion, i.e., the conversion range larger than the range of small, low-precision into the high-precision type, the final result is the type of conversion. such as:

System.out.println (false 1.0:? 'A'); Output: 97.0

 

Better not so Kazakhstan has been next to useless prompt dead code [the code]

Guess you like

Origin www.cnblogs.com/yundong333/p/12108678.html