| CHAPTER 3: Lexical Structure |
Previous |
Java Language |
Index |
Next |
A literal is the source code representation of a value of a primitive type (S4.2), the String type (S4.3.3, S20.12), or the null type (S4.1):
Literal: IntegerLiteral FloatingPointLiteral BooleanLiteral CharacterLiteral StringLiteral NullLiteral
See S4.2.1 for a general discussion of the integer types and values.
An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), or octal (base 8):
IntegerLiteral: DecimalIntegerLiteral HexIntegerLiteral OctalIntegerLiteral DecimalIntegerLiteral: DecimalNumeral IntegerTypeSuffixopt HexIntegerLiteral: HexNumeral IntegerTypeSuffixopt OctalIntegerLiteral: OctalNumeral IntegerTypeSuffixopt IntegerTypeSuffix: one of l L
An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (S4.2.1). The suffix L is preferred, because the letter l (ell) is often hard to distinguish from the digit 1 (one).
A decimal numeral is either the single ASCII character 0 , representing the integer zero, or consists of an ASCII digit from 1 to 9 , optionally followed by one or more ASCII digits from 0 to 9 , representing a positive integer:
DecimalNumeral: 0 NonZeroDigit Digitsopt Digits: Digit Digits Digit Digit: 0 NonZeroDigit NonZeroDigit: one of 1 2 3 4 5 6 7 8 9
A hexadecimal numeral consists of the leading ASCII characters 0x or 0X followed by one or more ASCII hexadecimal digits and can represent a positive, zero, or negative integer. Hexadecimal digits with values 10 through 15 are represented by the ASCII letters a through f or A through F , respectively; each letter used as a hexadecimal digit may be uppercase or lowercase.
HexNumeral: 0 x HexDigit 0 X HexDigit HexNumeral HexDigit
The following production from S3.3 is repeated here for clarity:
HexDigit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.
OctalNumeral: 0 OctalDigit OctalNumeral OctalDigit OctalDigit: one of 0 1 2 3 4 5 6 7
Note that octal numerals are always consist of two or more digits; 0 is always considered to be a decimal numeral-not that it matters much in practice, for the numerals 0 , 00 , and 0x0 all represent exactly the same integer value.
The largest decimal literal of type int
is 2147483648
(
). All decimal literals from 0
to 2147483647
may appear anywhere an int
literal may appear, but the literal 2147483648
may appear only as the operand of the unary negation operator -
.
The largest positive hexadecimal and octal literals of type int
are 0x7fffffff
and 017777777777
, respectively, which equal 2147483647
(
). The most negative hexadecimal and octal literals of type int
are 0x80000000
and 020000000000
, respectively, each of which represents the decimal value -2147483648
(
). The hexadecimal and octal literals 0xffffffff
and 037777777777
, respectively, represent the decimal value -1
.
See also Integer.MIN_VALUE (S20.7.1) and Integer.MAX_VALUE (S20.7.2).
A compile-time error occurs if a decimal literal of type int
is larger than 2147483648
(
), or if the literal 2147483648
appears anywhere other than as the operand of the unary -
operator, or if a hexadecimal or octal int
literal does not fit in 32 bits.
Examples of int literals:
0 2 0372 0xDadaCafe 1996 0x00FF00FF
The largest decimal literal of type long
is 9223372036854775808L
(
). All decimal literals from 0L
to 9223372036854775807L
may appear anywhere a long
literal may appear, but the literal 9223372036854775808L
may appear only as the operand of the unary negation operator -
.
The largest positive hexadecimal and octal literals of type long
are 0x7fffffffffffffffL
and 0777777777777777777777L
, respectively, which equal 9223372036854775807L
(
). The literals 0x8000000000000000L
and 01000000000000000000000L
are the most negative long
hexadecimal and octal literals, respectively. Each has the decimal value -9223372036854775808L
(
). The hexadecimal and octal literals 0xffffffffffffffffL
and 01777777777777777777777L
, respectively, represent the decimal value -1L
.
See also Long.MIN_VALUE (S20.8.1) and Long.MAX_VALUE (S20.8.2).
A compile-time error occurs if a decimal literal of type long
is larger than 9223372036854775808L
(
), or if the literal 9223372036854775808L
appears anywhere other than as the operand of the unary -
operator, or if a hexadecimal or octal long
literal does not fit in 64 bits.
Examples of long literals:
0l 0777L 0x100000000L 2147483648L 0xC0B0L
See S4.2.3 for a general discussion of the floating-point types and values.
A floating-point literal has the following parts: a whole-number part, a decimal point (represented by an ASCII period character), a fractional part, an exponent, and a type suffix. The exponent, if present, is indicated by the ASCII letter e or E followed by an optionally signed integer.
At least one digit, in either the whole number or the fraction part, and either a decimal point, an exponent, or a float type suffix are required. All other parts are optional.
A floating-point literal is of type float if it is suffixed with an ASCII letter F or f ; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d .
FloatingPointLiteral: Digits . Digitsopt ExponentPartopt FloatTypeSuffixopt . Digits ExponentPartopt FloatTypeSuffixopt Digits ExponentPart FloatTypeSuffixopt Digits ExponentPartopt FloatTypeSuffix ExponentPart: ExponentIndicator SignedInteger ExponentIndicator: one of e E SignedInteger: Signopt Digits Sign: one of + - FloatTypeSuffix: one of f F d D
The Java types float and double are IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point values, respectively.
The details of proper input conversion from a Unicode string representation of a floating-point number to the internal IEEE 754 binary floating-point representation are described for the methods valueOf of class Float (S20.9.17) and class Double (S20.10.16) of the package java.lang .
The largest positive finite float literal is 3.40282347e+38f . The smallest positive finite nonzero literal of type float is 1.40239846e-45f . The largest positive finite double literal is 1.79769313486231570e+308 . The smallest positive finite nonzero literal of type double is 4.94065645841246544e-324 .
See Float.MIN_VALUE (S20.9.1) and Float.MAX_VALUE (S20.9.2); see also Double.MIN_VALUE (S20.10.1) and Double.MAX_VALUE (S20.10.2).
A compile-time error occurs if a nonzero floating-point literal is too large, so that on rounded conversion to its internal representation it becomes an IEEE 754 infinity. A Java program can represent infinities without producing a compile-time error by using constant expressions such as 1f/0f or -1d/0d or by using the predefined constants POSITIVE_INFINITY and NEGATIVE_INFINITY of the classes Float (S20.9) and Double (S20.10).
A compile-time error occurs if a nonzero floating-point literal is too small, so that, on rounded conversion to its internal representation, it becomes a zero. A compile-time error does not occur if a nonzero floating-point literal has a small value that, on rounded conversion to its internal representation, becomes a nonzero denormalized number.
Predefined constants representing Not-a-Number values are defined in the classes Float and Double as Float.NaN (S20.9.5) and Double.NaN (S20.10.5).
Examples of float literals:
1e1f 2.f .3f 0f 3.14f 6.022137e+23f
Examples of double literals:
1e1 2. .3 0.0 3.14 1e-9d 1e137
There is no provision for expressing floating-point literals in other than decimal radix. However, method intBitsToFloat (S20.9.23) of class Float and method longBitsToDouble (S20.10.22) of class Double provide a way to express floating-point values in terms of hexadecimal or octal integer literals. For example, the value of:
Double.longBitsToDouble(0x400921FB54442D18L)
is equal to the value of Math.PI (S20.11.2).
The boolean type has two values, represented by the literals true and false , formed from ASCII letters.
A boolean literal is always of type boolean .
BooleanLiteral: one of true false
A character literal is expressed as a character or an escape sequence, enclosed in ASCII single quotes. (The single-quote, or apostrophe, character is \u0027 .)
A character literal is always of type char .
CharacterLiteral: ' SingleCharacter ' ' EscapeSequence ' SingleCharacter: InputCharacter but not ' or \
The escape sequences are described in S3.10.6.
As specified in S3.4, the characters CR and LF are never an InputCharacter; they are recognized as constituting a LineTerminator.
It is a compile-time error for the character following the SingleCharacter or EscapeSequence to be other than a ' .
It is a compile-time error for a line terminator to appear after the opening ' and before the closing ' .
The following are examples of char literals:
'a' '%' '\t' '\\' '\'' '\u03a9' '\uFFFF' '\177'
Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in translation step 1 (S3.3) and the linefeed becomes a LineTerminator in step 2 (S3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n' (S3.10.6). Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r' .
In C and C++, a character literal may contain representations of more than one character, but the value of such a character literal is implementation-defined. In Java, a character literal always represents exactly one character.
A string literal consists of zero or more characters enclosed in double quotes. Each character may be represented by an escape sequence.
A string literal is always of type String (S4.3.3, S20.12). A string literal always refers to the same instance (S4.3.1) of class String .
StringLiteral: " StringCharactersopt " StringCharacters: StringCharacter StringCharacters StringCharacter StringCharacter: InputCharacter but not " or \ EscapeSequence
The escape sequences are described in S3.10.6.
As specified in S3.4, neither of the characters CR and LF is ever considered to be an InputCharacter; each is recognized as constituting a LineTerminator.
It is a compile-time error for a line terminator to appear after the opening " and before the closing matching " . A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator + (S15.17.1).
The following are examples of string literals:
"" // the empty string "\"" // a string containing " alone "This is a string" // a string containing 16 characters "This is a " + // actually a string-valued constant expression, "two-line string" // formed from two string literals
Because Unicode escapes are processed very early, it is not correct to write "\u000a" for a string literal containing a single linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in translation step 1 (S3.3) and the linefeed becomes a LineTerminator in step 2 (S3.4), and so the string literal is not valid in step 3. Instead, one should write "\n" (S3.10.6). Similarly, it is not correct to write "\u000d" for a string literal containing a single carriage return (CR). Instead use "\r" .
Each string literal is a reference (S4.3) to an instance (S4.3.1, S12.5) of class String (S4.3.3,S20.12). String objects have a constant value. String literals-or, more generally, strings that are the values of constant expressions (S15.27)-are "interned" so as to share unique instances, using the method String.intern (S20.12.47).
Thus, the test program consisting of the compilation unit (S7.3):
package testPackage;
class Test {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.print((hello == "Hello") + " ");
System.out.print((Other.hello == hello) + " ");
System.out.print((other.Other.hello == hello) + " ");
System.out.print((hello == ("Hel"+"lo")) + " ");
System.out.print((hello == ("Hel"+lo)) + " ");
System.out.println(hello == ("Hel"+lo).intern());
}
}
class Other { static String hello = "Hello"; }
and the compilation unit:
package other;
public class Other { static String hello = "Hello"; }
produces the output:
true true true true false true
This example illustrates six points:
The character and string escape sequences allow for the representation of some nongraphic characters as well as the single quote, double quote, and backslash characters in character literals (S3.10.4) and string literals (S3.10.5).
EscapeSequence:
\ b /* \u0008: backspace BS */ \ t /* \u0009: horizontal tab HT */ \ n /* \u000a: linefeed LF */ \ f /* \u000c: form feed FF */ \ r /* \u000d: carriage return CR */ \ " /* \u0022: double quote " */ \ ' /* \u0027: single quote ' */ \ \ /* \u005c: backslash \ */ OctalEscape /* \u0000 to \u00ff: from octal value */
OctalEscape: \ OctalDigit \ OctalDigit OctalDigit \ ZeroToThree OctalDigit OctalDigit OctalDigit: one of 0 1 2 3 4 5 6 7 ZeroToThree: one of 0 1 2 3
It is a compile-time error if the character following a backslash in an escape is not an ASCII b , t , n , f , r , " , ' , \ , 0 , 1 , 2 , 3 , 4 , 5 , 6 , or 7 . The Unicode escape \u is processed earlier (S3.3). (Octal escapes are provided for compatibility with C, but can express only Unicode values \u0000 through \u00FF , so Unicode escapes are usually preferred.)
The null type has one value, the null reference, represented by the literal null , which is formed from ASCII characters. A null literal is always of the null type.
NullLiteral: null
| © 1996 Sun Microsystems, Inc. All rights reserved. |