Java Reference
In-Depth Information
vary outside of a range of 1 to 1000, then a two-byte integer ( short ) is large
enough to accommodate it. On the other hand, when it's not clear what the range
of a particular variable will be, we should provide a reasonable, even generous,
amount of space. In most situations memory space is not a serious restriction, and
we can usually afford generous assumptions.
Note that even though a float value supports very large (and very small) num-
bers, it has only seven significant digits. Therefore, if it is important to accurately
maintain a value such as 50341.2077, we need to use a double .
As we've already discussed, a literal is an explicit data value used in a pro-
gram. The various numbers used in programs such as Facts and Addition and
PianoKeys are all integer literals. Java assumes all integer literals are of type int ,
unless an L or l is appended to the end of the value to indicate that it should be
considered a literal of type long , such as 45L .
Likewise, Java assumes that all floating point literals are of type double . If we
need to treat a floating point literal as a float , we append an F or f to the end
of the value, as in 2.718F or 123.45f . Numeric literals of type double can be fol-
lowed by a D or d if desired.
Decimal Integer Literal
0
1 - 9
L
l
0 - 9
An integer literal is composed of a series of digits followed by
an optional suffix to indicate that it should be considered a integer.
Negation of a literal is considered a separate operation.
Examples:
5
2594
4920328L
The following are examples of numeric variable declarations in Java:
int answer = 42;
byte smallNumber1, smallNumber2;
long countedStars = 86827263927L;
float ratio = 0.2363F;
double delta = 453.523311903;
 
 
Search WWH ::




Custom Search