When you explicitly assign a value to a variable in the code, as
in
double
x = 3.1;
int i = 43;
the compiler needs to translate the character strings "3.1"
and "43" into numerical values of a particular type. Such
explicit values in a computer language are called "literals"
(for the obvious reason that they are literally equivalent
to their stated value.)
(In the bytecode each literal in your class code gets put into
a so-called constants pool.)
Floating Point Literals
The compiler will see the decimal point and decide that the string
"3.1" is a valid floating point string. It must next decide
what type to assign it. In Java a floating point literal defaults
to the double
type.
double
x = 3.1;
Since converting a double to float is a narrowing
conversion, the following assignment
float
y = 3.1; // results in an error
message
causes an error. You must instead explicitly type the floating
point literal,
float
y = 3.1f;
by appending the "f" character to the value.
The following examples illustrate how to express floating point
exponents in a literal:
double
y = 3.1e10;
y = 3.1e-10;
y = 3E10;
y = -3.1e05;
Integer Literals
Integer literals default to the int
type.
int
i = 3;
// 3 is an integer literal
You can assign a long
type to the int
literal since this will result in a widening conversion, which does
not require an explicit cast.
long
m = 3;
// allowed
You might think, then, that the following narrowing conversions
would result in an error
byte
b = -2;
short i = 3;
but Java allows this if the literals are within the allowed
ranges for byte
and short
types. However, this only works with the direct literal assignment.
An assignment to an int
variable,
int
i = 3;
short i = i; //
error
i = -2;
byte b = i; //
error
will result in an error message.
You can explicitly specify a long literal by appending a 'L' or
'l':
long
m = 2147483648L;
Besides the decimal base, you can use hexadecimal and octal formats
for the integer literals:
short
i = 0x00AF; // hex
for 175
byte b = 017; // octal for 15
where hex numbers begin with "0x" and octal with "0".
Note: Assigning a literal
value larger than the range for a particular integer type will
NOT result in an overflow warning (and no underflow warning
for large negative values.) Instead the value will wrap around
to the lowest value. For example,
int
i = 130;
byte bb = (byte)i;
results in a value of -126 in the bb variable.
Characters and String Literals
You must bracket a character literal with single
quotation marks.
char
c = '*';
char c1 = 'A';
char c2 = "A"; //
error
A string literal, however, requires double
quotation marks:
String
str = "abc";
String str1 = "Java";
String
str2 = 'J'; //
error
Special Literals
For boolean type data, only two types of data are
available: true
and false.
So these literals are defined already in the core language:
Boolean
b = true;
Boolean b1 = false;
Similarly, for references to objects, the null
literal can be used when no object is yet chosen for the reference
variable:
String
str = null;
References & Web Resources
Latest update: Oct. 14, 2004
|