The conditional operator ?:
uses the boolean
value of its first operand to decide which of the other two expressions will be evaluated. (See §15.25, "Conditional Operator ?:
," of the Java Language Specification [JLS 2005].)
...
The Java Language Specification rules for determining the result type of result of a conditional expression (tabulated belowsee following table) are complicated; programmers could be surprised by the type conversions required for expressions they have written.
Result type determination begins from the top of the table; the compiler applies the first matching rule. The " Operand 2 " and " Operand 3 " columns refer to operand2
and operand3
(from the above previous definition) , respectively. In the table, constant int
refers to constant expressions of type int
(such as '0' or variables declared final
).
...
See §5.1.7, "Boxing Conversion,"; §5.1.10, "Capture Conversion,"; and §15.12.2.7, "Inferring Type Arguments Based on Actual Arguments," of the Java Language Specification for additional information on the final table entry.
...
In this noncompliant code example, the programmer expects that both print statements will print the value of alpha
as a char
— A
. The first print statement does print prints A
because the compiler applies the eighth rule from the result type determination table to determine that the second and third operands of the conditional expression are, or are converted to, type char
. However, the second print statement prints 65
— the value of alpha
as an int
. The first matching rule from the table above is the tenth rule; consequently, the compiler promotes the value of alpha
to type int
.
...
Note that the explicit cast in the first conditional expression is redundant; that is, the value printed remains identical whether the cast is present or absent. Nevertheless, use of the redundant cast is good practice; it serves as an explicit indication of the programmer's intent and , consequently , improves maintainability. When the value of i
in the second conditional expression falls outside the range that can be represented as a char
, the explicit cast will truncate its value. This usage complies with exception EXP13-EX1 of guideline " NUM00-J. Ensure conversions of numeric types to narrower types do not result in lost or misinterpreted data."
Noncompliant Code Example
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="798c816688019071-a3e7c4e0-48cc4417-acf9ba0e-288152099bb1233e18117a9a"><ac:plain-text-body><![CDATA[ | [[Bloch 2005 | AA. References#Bloch 05]] | Puzzle 8: Dos Equis | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="8b79368a65eaa22c-bc85379f-4a434676-9bafaf0e-ae1e92ffbb9d75dc03942aa7"><ac:plain-text-body><![CDATA[ | [[Findbugs 2008 | AA. References#Findbugs 08]] | "Bx: Primitive value is unboxed and coerced for ternary operator" | ]]></ac:plain-text-body></ac:structured-macro> | |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cf5e189eb5372e11-4acdcf97-489b45cc-b8ec8f25-e08bcb9565e4275e809b3d02"><ac:plain-text-body><![CDATA[ | [[JLS 2005 | AA. References#JLS 05]] | [§15.25, "Conditional Operator | http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25] | ]]></ac:plain-text-body></ac:structured-macro> |
...