Wednesday, 23 March 2011

java literals

3.10 Literals
A literal is the source code representation of a value of a primitive type (§4.2), the String type (§4.3.3), or the null type (§4.1):
Literal:
Integer Literal
Floating Point Literal
Boolean Literal
Character Literal
String Literal
Null Literal
3.10.1 Integer Literals LEXICAL STRUCTURE
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 (§4.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:
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.

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.

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.
3.10.2 Floating-Point Literals
See §4.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 or hexadecimal point (represented by an ASCII period character), a fractional part, an exponent, and a type suffix.
A floating point number may be written either as a decimal value or as a hexadecimal value.
For decimal literals, the exponent, if present, is indicated by the ASCII letter e or E followed by an optionally signed integer.
For hexadecimal literals, the exponent is always required and is indicated by the ASCII letter p or P followed by an optionally signed integer.
For decimal floating-point literals, 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.
For hexadecimal floating-point literals, at least one digit is required in either the whole number or fraction part, the exponent is mandatory, and the float type suffix is optional.
A floating-point literal is of type float if it is suffixed with an ASCII letter For f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.
FloatingPointLiteral:
DecimalFloatingPointLiteral
HexadecimalFloatingPointLiteral
DecimalFloatingPointLiteral:
Digits . Digitsopt ExponentPartopt FloatTypeSuffixopt
. Digits ExponentPartopt FloatTypeSuffixopt
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
HexadecimalFloatingPointLiteral:
HexSignific and BinaryExponent FloatTypeSuffixopt

HexSignificand:
HexNumeral
HexNumeral .
0x HexDigitsopt . HexDigits
0X HexDigitsopt . HexDigits
BinaryExponent:
BinaryExponentIndicator SignedInteger
BinaryExponentIndicator:one of
p P



3.10.3 Boolean Literals
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

3.10.4 Character Literals
3.10.5 String Literals
3.10.6 Escape Sequences for Character and String Literals

No comments:

Post a Comment