Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Parasoft Jtest 2022.2

...

Determining the Result Type of a Conditional Expression

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 second and third operands

11

T1 = boxing conversion(S1)

T2 = boxing conversion(S2)

Apply capture conversion to lub(T1,T2)

The complexity of the rules that determine the result type of a conditional expression can lead to unintended type conversions. Consequently, the second and third operands of each conditional expression should have identical types. This recommendation also applies to boxed primitives.

...

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

Automated Detection

ToolVersionCheckerDescription
Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.EXP55.COMTAvoid using the conditional operator with mismatched numeric types

Bibliography

[Bloch 2005]

Puzzle 8, "Dos Equis"

[Findbugs 2008]

"Bx: Primitive Value Is Unboxed and Coerced for Ternary Operator"

[JLS 2013]

§15.25, "Conditional Operator ? :"

...


...

Image Modified Image Modified Image Modified