Java Reference
In-Depth Information
Declaring literals
Java programs usually contain values expressed directly. These values are
called literal expressions ,or literals . You use a literal to initialize a vari-
able or a constant. For example:
int age = 32;
String myU = “Minnesota State University”;
In the declaration of literal values, Java assumes that floating-point lit-
erals are of type double. For this reason, the statement:
float aVal = 12.33;
generates an error. The reason for the error is that Java assumed that the
value 12.33 is in double format, and there could be loss of precision when it
is converted into the float type of the variable. The solution is to force the
literal, in this case the value 12.33, into a float type, as follows:
float aVal = (float) 12.33;
Java also provides the following shorthand:
float aVal = 12.33f;
The small-case letter f following the literal value performs a type cast.
Search WWH ::




Custom Search