Type ternary operator conversion of

--- --- restore content begins

System.out.println(3<2?'a':98)

--- end --- restore content

System.out.println (? 3 <2 'a ': 98) 

ternary operator as consistent as possible types of two operands, to avoid abnormal

attachment: conversion rule

    when the two operands can not be converted, the conversion is not, Object type return value
    if both operands are explicit type, according to the data type conversion rules based processes (not including boolean)
    conversion rule: from small to large storage region of memory type range type.
    Specific rules: byte → short (char) → int → long → float → double
follows:

(1) If expression 1 and expression 2 of the same type, then the type entire operator condition is the result of this type.

(2) if a type of the expression is T, T is the byte or short or char, the expression of the other type are constant expression of type int, and the value of the constant expression is represented by T type (also that is, the value of the constant expression is in the range of type T), then the result of the conditional operator entire type is T.

(3) In addition to the above, if the expression 1 and the different types of expression 2, it will be the type of lift (small turn large), the type of the result of the type on the entire conditional operator is improved.


 

Guess you like

Origin www.cnblogs.com/wskb/p/10959412.html