An integer literal can always be assigned to a long variable. However, to specify a long
literal, you will need to explicitly tell the compiler that the literal value is of type long. You
do this by appending an upper- or lowercase L to the literal. For example, 0x7ffffffffffffffL
or 9223372036854775807L is the largest long. An integer can also be assigned to a char as
long as it is within range.
Floating-Point Literals
Floating-point numbers represent decimal values with a fractional component. They can be
expressed in either standard or scientific notation. Standard notation consists of a whole number
component followed by a decimal point followed by a fractional component. For example, 2.0,
3.14159, and 0.6667 represent valid standard-notation floating-point numbers. Scientific notation
uses a standard-notation, floating-point number plus a suffix that specifies a power of 10 by
which the number is to be multiplied. The exponent is indicated by an E or e followed by a
decimal number, which can be positive or negative. Examples include 6.022E23, 314159E­05,
and 2e+100.
Floating-point literals in Java default to double precision. To specify a float literal, you
must append an F or f to the constant. You can also explicitly specify a double literal by
appending a D or d. Doing so is, of course, redundant. The default double type consumes 64
bits of storage, while the less-accurate float type requires only 32 bits.
Boolean Literals
Boolean literals are simple. There are only two logical values that a boolean value can have,
true and false. The values of true and false do not convert into any numerical representation.
The true literal in Java does not equal 1, nor does the false literal equal 0. In Java, they can only
be assigned to variables declared as boolean, or used in expressions with Boolean operators.
Character Literals
Characters in Java are indices into the Unicode character set. They are 16-bit values that can
be converted into integers and manipulated with the integer operators, such as the addition
and subtraction operators. A literal character is represented inside a pair of single quotes. All
of the visible ASCII characters can be directly entered inside the quotes, such as `a', `z', and `@'.
For characters that are impossible to enter directly, there are several escape sequences that allow
you to enter the character you need, such as `\'' for the single-quote character itself and `\n' for
the newline character. There is also a mechanism for directly entering the value of a character in
octal or hexadecimal. For octal notation, use the backslash followed by the three-digit
number. For example, `\141' is the letter `a'. For hexadecimal, you enter a backslash-u (\u), then
exactly four hexadecimal digits. For example, `\u0061' is the ISO-Latin-1 `a' because the top byte
is zero. `\ua432' is a Japanese Katakana character. Table 3-1 shows the character escape sequences.
String Literals
String literals in Java are specified like they are in most other languages--by enclosing
a sequence of characters between a pair of double quotes. Examples of string literals are
"Hello World"
"two\nlines"
"\"This is in quotes\""
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home