-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathCondExprTypes.qhelp
More file actions
49 lines (35 loc) · 1.27 KB
/
CondExprTypes.qhelp
File metadata and controls
49 lines (35 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>Conditional expressions of the form <code>(p ? e1 : e2)</code> can
yield unexpected results if <code>e1</code> and <code>e2</code> have
distinct primitive types.</p>
</overview>
<example>
<p>The following example illustrates the most confusing case, which occurs when one branch has type <code>char</code>
and the other branch does not have type <code>char</code>.</p>
<sample src="CondExprTypes.java" />
<p>This unexpected result is due to binary numeric promotion
of <code>'x'</code> from <code>char</code> to <code>int</code>.
For details on the result type of
the conditional operator, see the references.
</p>
</example>
<recommendation>
<p>When using the ternary conditional operator with numeric operands,
the second and third operand should have the same numeric type.
This avoids potentially unexpected results caused by binary numeric promotion.
</p>
</recommendation>
<references>
<li>
J. Bloch and N. Gafter, <em>Java Puzzlers: Traps, Pitfalls, and Corner Cases</em>, Puzzle 8. Addison-Wesley, 2005.
</li>
<li>
Java Language Specification:
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-15.html#jls-15.25">Conditional Operator ? :</a>.
</li>
</references>
</qhelp>