Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

The conditional operator ?: uses the boolean value of its first operand to decide which of the other two expressions will be evaluated. (See §15§15.25, "Conditional Operator ?:" of the Java Language Specification [JLS 2005].)

...

Rule

Operand 2

Operand 3

Resultant type

1

type T

type T

type T

2

boolean

Boolean

boolean

3

Boolean

boolean

boolean

4

null

reference

reference

5

reference

null

reference

6

byte or Byte

short or Short

short

7

short or Short

byte or Byte

short

8

byte, short, char, Byte, Short, Character

constant int

byte, short, char if value of int is representable

9

constant int

byte, short, char, Byte, Short, Character

byte, short, char if value of int is representable

10

other numeric

other numeric

promoted type of the 2nd and 3rd operands

11

T1 = boxing conversion (S1)

T2 = boxing conversion(S2)

apply capture conversion to lub(T1,T2)

See §5§5.1.7, "Boxing Conversion"; §5§5.1.10, "Capture Conversion"; and §15§15.12.2.7, "Inferring Type Arguments Based on Actual Arguments" of the Java Language Specification for additional information on the final table entry.

...

Automated detection of condition expressions whose second and third operands are of different types is straightforward.

Bibliography

<ac:structured-macro ac:name="unmigrated-wiki-markup

...

" ac:schema-version="1" ac:macro-id="d4f298aa-16c0-4f91-82f6-bfc13351d4d7"><ac:plain-text-body><![CDATA[

[[Bloch

...

2005

...

AA.

...

Bibliography#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="611892b1-f615-4e65-b8d8-bf00a1678432"><ac:plain-text-body><![CDATA[

[[Findbugs 2008

AA. Bibliography#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="5de9d3cf-680c-4ed7-b4b3-1ba503572a93"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#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>

...

EXP11-J. Never dereference null pointers      02. Expressions (EXP)      03. Numeric Types and Operations (NUM)