Java Reference
In-Depth Information
Unfortunately, it doesn't compile. The constant declaration is broken, and the compiler will tell you
the problem: 0x90 is not a valid value for the type byte . If you fix the declaration as follows, the
program will work fine:
private static final byte TARGET = (byte) 0x90;
To summarize: Avoid mixed-type comparisons, because they are inherently confusing ( Puzzle
5 ). To help achieve this goal, use declared constants in place of "magic numbers." You already
knew that this was a good idea; it documents the meanings of constants, centralizes their
definitions, and eliminates duplicate definitions. Now you know that it also forces you to give each
constant a type appropriate for its use, eliminating one source of mixed-type comparisons.
The lesson for language designers is that sign extension of byte values is a common source of bugs
and confusion. The masking that is required in order to suppress sign extension clutters programs,
making them less readable. Therefore, the byte type should be unsigned. Also, consider providing
literals for all primitive types, reducing the need for error-prone type conversions ( Puzzle 27 ).
< Day Day Up >
 
 
Search WWH ::




Custom Search