Java Reference
In-Depth Information
Applicability
It is acceptable to use the ordinals associated with an enumerated type when the order of
the enumeration constants is standard and extra constants cannot be added. For example,
the use of ordinals is permitted with the following enumerated type:
Click here to view code image
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY }
In general, the use of ordinals to derive integer values reduces the program's maintain-
ability and can lead to errors in the program.
Bibliography
[API 2013]
Class Enum<E extends Enum<E>>
[Bloch 2008]
Item 31, “Use Instance Fields Instead of Ordinals”
[JLS 2013]
§8.9, “Enums”
29. Be aware of numeric promotion behavior
Numeric promotions are used to convert the operands of a numeric operator to a common
type so that an operation can be performed. When using arithmetic operators with mixed
operand sizes, narrower operands are promoted to the type of the wider operand.
Promotion Rules
The JLS, §5.6, “Numeric Promotions” [JLS 2013], describes numeric promotion as the
following:
1. If any of the operands is of a reference type, unboxing conversion is performed.
2. If either operand is of type double , the other is converted to double .
3. Otherwise, if either operand is of type float , the other is converted to float .
4. Otherwise, if either operand is of type long , the other is converted to long .
5. Otherwise, both operands are converted to type int .
Widening conversions, resulting from integer promotions, preserve the overall mag-
nitude of the number. However, promotions in which the operands are converted from an
Search WWH ::




Custom Search