(^106) | Arithmetic Expressions
Literal Type
0.0 double
0.0f float
2.001E3 double
2.001E3F float
1.8E225F invalid (the exponent 225 is too large for the type float)
We’ll discuss floating-point numbers in more detail in Chapter 12, but there is one more
thing you should know about them now. Computers cannot always represent floating-point
numbers exactly. In Chapter 1, you learned that the computer stores all data in binary (base–2)
form. Many decimal floating-point values can only be approximated in the binary number
system. Don’t be surprised if your application prints out the number 4.8 as 4.799998. In most
cases, slight inaccuracies in the rightmost fractional digits are to be expected and are not the
result of programmer error.
3.3 Declarations for Numeric Types
Just as with the types charand String, we can declare fields of type int,long,float, and dou-
ble. Such declarations use the same syntax introduced earlier, except that the literals and
the names of the data types are different.
Named Constant Declarations
In the case of named constant declarations, the literal values in the declarations are nu-
meric instead of being characters in single or double quotes. For example, here are some con-
stant declarations that define int,long,float, and doublevalues. For comparison, declarations
of charand Stringvalues are included.
final double PI = 3.14159;
final float E = 2.71828F;
final long MAX_TEMP =1000000000L;
final int MIN_TEMP =–273;
final char LETTER ='W';
finalString NAME = "Elizabeth";
Although we put character and string literals in quotes, we do not follow this approach
with literal integers and floating-point numbers, because there is no chance of confusing them
with identifiers. Why? Because identifiers must start with a letter or underscore, and num-
bers must start with a digit or sign.