Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: relatively minor edits

...

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 definition), respectively. In the table, constant int refers to constant expressions of type int (such as '0' or variables declared final).

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)

...

In this noncompliant code example, the programmer expects that both print statements will print the value of alpha as a charA. The first print statement indeed prints does print 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.

...

This noncompliant code example prints 100 as the size of the HashSet rather than the expected result (some value between 0 and 50). The combination of values of types short and int in the second argument of the conditional expression (the operation i-1) causes the result to be an int as specified by the normal integer promotion rules. Consequently, the Short object in the third argument is autounboxed into a short, which is then promoted into an int. The result of the conditional expression is then autoboxed into an object of type Integer. Because the HashSet contains only values of type Short, the call to HashSet.remove() has no effect.

...

This compliant solution casts the second operant operand to type short, then explicitly invokes the Short.valueOf method to create a Short instance whose value is i - 1. Consequently, the second and third operands of the conditional expression both have type Short, and the remove() call has the expected effectresult.

Code Block
bgColor#ccccff
public class ShortSet {
  public static void main(String[] args) {
    HashSet<Short> s = new HashSet<Short>();
      for (short i = 0; i < 100; i++) {
        s.add(i);
        // Cast of i-1 is safe, because value is always representable
        Short workingVal = (short) (i-1);
        ... // other code may update workingVal

        // Cast of i-1 is safe, because value is always representable
        s.remove(((i & 1) == 0) ? Short.valueOf((short) (i-1)) : workingVal);
      }
    System.out.println(s.size());
  }
}

...

When the second and third operands of a conditional expression have different types, they can be subject to unexpected type conversions that were not anticipated by the programmer.

Guideline

Severity

Likelihood

Remediation Cost

Priority

Level

EXP14-J

low

unlikely

medium

P2

L3

...