首页 > 代码库 > Java文法(7)—Literals
Java文法(7)—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: IntegerLiteral FloatingPointLiteral BooleanLiteral CharacterLiteral StringLiteral NullLiteral---------------------------------------------------------------------------------------------------------------------------------------扩展一(Integer Literals): ------------------------------------------------------------------------------------------------------------------------ 说明: An integer literal may be expressed in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2). The largest decimal literal of type int is 2147483648 (2^31). All decimal literals from 0 to 2147483647 may appear anywhere an int literal may appear. It is a compile-time error if a decimal literal of type int is larger than 2147483648 (2^31), or if the decimal literal 2147483648 appears anywhere other than as the operand of the unary minus operator (§15.15.4). The largest positive hexadecimal, octal, and binary literals of type int - each of which represents the decimal value 2147483647 (2^31-1) - are respectively: • 0x7fff_ffff, • 0177_7777_7777, and • 0b0111_1111_1111_1111_1111_1111_1111_1111 The most negative hexadecimal, octal, and binary literals of type int - each of which represents the decimal value -2147483648 (-2^31) - are respectively: • 0x8000_0000, • 0200_0000_0000, and • 0b1000_0000_0000_0000_0000_0000_0000_0000 ----------------------------------------------------------------------------------------------------------------------------- 文法: IntegerLiteral: DecimalIntegerLiteral HexIntegerLiteral OctalIntegerLiteral BinaryIntegerLiteral DecimalIntegerLiteral: DecimalNumeral IntegerTypeSuffix opt HexIntegerLiteral: HexNumeral IntegerTypeSuffix opt OctalIntegerLiteral: OctalNumeral IntegerTypeSuffix opt BinaryIntegerLiteral: BinaryNumeral IntegerTypeSuffix opt IntegerTypeSuffix: one of l L ------------------------------------------------------------------------- DecimalNumeral: 0 NonZeroDigit Digits opt NonZeroDigit Underscores Digits Digits: Digit Digit DigitsAndUnderscores opt Digit Digit: 0 NonZeroDigit NonZeroDigit: one of 1 2 3 4 5 6 7 8 9 DigitsAndUnderscores: DigitOrUnderscore DigitsAndUnderscores DigitOrUnderscore DigitOrUnderscore: Digit _ Underscores: _ Underscores _ -------------------------------------------------------------------------- HexNumeral: 0x HexDigits 0X HexDigits HexDigits: HexDigit HexDigit HexDigitsAndUnderscores opt HexDigit 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 HexDigitsAndUnderscores: HexDigitOrUnderscore HexDigitsAndUnderscores HexDigitOrUnderscore HexDigitOrUnderscore: HexDigit _ ---------------------------------------------------------------------------------- OctalNumeral: 0 OctalDigits 0 Underscores OctalDigits OctalDigits: OctalDigit OctalDigit OctalDigitsAndUnderscores opt OctalDigit OctalDigit: one of 0 1 2 3 4 5 6 7 OctalDigitsAndUnderscores: OctalDigitOrUnderscore OctalDigitsAndUnderscores OctalDigitOrUnderscore OctalDigitOrUnderscore: OctalDigit _ ----------------------------------------------------------------------------------- BinaryNumeral: 0b BinaryDigits 0B BinaryDigits BinaryDigits: BinaryDigit BinaryDigit BinaryDigitsAndUnderscores opt BinaryDigit BinaryDigit: one of 0 1 BinaryDigitsAndUnderscores: BinaryDigitOrUnderscore BinaryDigitsAndUnderscores BinaryDigitOrUnderscore BinaryDigitOrUnderscore: BinaryDigit _------------------------------------------------------------------------------------------------------------------------------------------扩展二(Floating-Point Literals): ------------------------------------------------------------------------------------------------------------------------------------- 说明: 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. The elements of the types float and double are those values that can be represented using the IEEE 754 32-bit single-precision and 64-bit double-precision binary floating-point formats, 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 and class Double of the package java.lang. The largest positive finite literal of type float is 3.4028235e^38f. The smallest positive finite non-zero literal of type float is 1.40e^-45f. The largest positive finite literal of type double is 1.7976931348623157e^308. The smallest positive finite non-zero literal of type double is 4.9e^-324. It is a compile-time error if a non-zero floating-point literal is too large, so that on rounded conversion to its internal representation, it becomes an IEEE 754 infinity. A 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 and Double. It is a compile-time error if a non-zero floating-point literal is too small, so that, on rounded conversion to its internal representation, it becomes a zero. ------------------------------------------------------------------------------------------------------------------------------- 文法: FloatingPointLiteral: DecimalFloatingPointLiteral HexadecimalFloatingPointLiteral DecimalFloatingPointLiteral: Digits . Digits opt ExponentPart opt FloatTypeSuffix opt . Digits ExponentPart opt FloatTypeSuffix opt Digits ExponentPart FloatTypeSuffix opt Digits ExponentPart opt FloatTypeSuffix ExponentPart: ExponentIndicator SignedInteger ExponentIndicator: one of e E SignedInteger: Sign opt Digits Sign: one of + - FloatTypeSuffix: one of f F d D HexadecimalFloatingPointLiteral: HexSignificand BinaryExponent FloatTypeSuffix opt HexSignificand: HexNumeral HexNumeral . 0x HexDigitsopt . HexDigits 0X HexDigitsopt . HexDigits BinaryExponent: BinaryExponentIndicator SignedInteger BinaryExponentIndicator:one of p P ------------------------------------------------------------------------------------------------------------------------------------- 例子: 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------------------------------------------------------------------------------------------------------------------------------------扩展三(Boolean Literals): ----------------------------------------------------------------------------------------------------------------------- 说明: The boolean type has two values, represented by the boolean literals true and false , formed from ASCII letters. A boolean literal is always of type boolean . ------------------------------------------------------------------------------------------------------------------------- 文法: BooleanLiteral: one of true false-----------------------------------------------------------------------------------------------------------------------------------------扩展四(Character Literals): ------------------------------------------------------------------------------------------------------------------------ 说明: A character literal is expressed as a character or an escape sequence (§3.10.6), enclosed in ASCII single quotes. (The single-quote, or apostrophe, character is \u0027 .) Character literals can only represent UTF-16 code units (§3.1), i.e., they are limited to values from \u0000 to \uffff . Supplementary characters must be represented either as a surrogate pair within a char sequence, or as an integer, depending on the API they are used with. A character literal is always of type char . 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 (§3.4) to appear after the opening ‘ and before the closing ‘ . 注: 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 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence ‘\n‘ (§3.10.6). Similarly, it is not correct to write ‘\u000d‘ for a character literal whose value is carriage return (CR). Instead, use ‘\r‘ . --------------------------------------------------------------------------------------------------------------------------- 文法: CharacterLiteral: ‘ SingleCharacter ‘ ‘ EscapeSequence ‘ SingleCharacter: InputCharacter but not ‘ or -------------------------------------------------------------------------------------------------------------------------------- 使用例子: • ‘a‘ • ‘%‘ • ‘\t‘ • ‘\\‘ • ‘\‘‘ • ‘\u03a9‘ • ‘\uFFFF‘ • ‘\177‘ • ‘Ω‘ • ‘⊗‘-------------------------------------------------------------------------------------------------------------------------------------------扩展五(String Literals): -------------------------------------------------------------------------------------------------------------------------------------- 说明: A string literal consists of zero or more characters enclosed in double quotes. Characters may be represented by escape sequences (§3.10.6) - one escape sequence for characters in the range U+0000 to U+FFFF, two escape sequences for the UTF-16 surrogate code units of characters in the range U+010000 to U +10FFFF. It is a compile-time error for a line terminator to appear after the opening " and before the closing matching " . As specified in §3.4, the characters CR and LF are never an InputCharacter; each is recognized as constituting a LineTerminator. A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator + (§15.18.1). 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 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the string literal is not valid in step 3. Instead, one should write "\n" (§3.10.6). Similarly, it is not correct to write "\u000d" for a string literal containing a single carriage return (CR). Instead, use "\r" . Finally, it is not possible to write "\u0022" for a string literal containing a double quotation mark ("). A string literal is a reference to an instance of class String (§4.3.1, §4.3.3). Moreover, a string literal always refers to the same instance of class String . This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern . 注: 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 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the string literal is not valid in step 3. Instead, one should write "\n" (§3.10.6). Similarly, it is not correct to write "\u000d" for a string literal containing a single carriage return (CR). Instead, use "\r" . Finally, it is not possible to write "\u0022" for a string literal containing a double quotation mark ("). ------------------------------------------------------------------------------------------------------------------------------------------- 文法: StringLiteral: " StringCharacters opt " StringCharacters: StringCharacter StringCharacters StringCharacter StringCharacter: InputCharacter but not " or EscapeSequence See §3.10.6 for the definition of EscapeSequence. ---------------------------------------------------------------------------------------------------------------------------------- 使用例子: "" //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 ---------------------------------------------------------------------------------------------------------------------------------- 扩展一(使相同的字符串文法运算结果表示相同的对象): ----------------------------------------------------------------------------------------------------------- 语法: String.intern ------------------------------------------------------------------------------------------------------------- 说明: A string literal is a reference to an instance of class String (§4.3.1, §4.3.3). Moreover, a string literal always refers to the same instance of class String . This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the method String.intern . -------------------------------------------------------------------------------------------------------------- 使用例子: The program consisting of the compilation unit (§7.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 { public static String hello = "Hello"; } produces the output: true true true true false true 说明: • Literal strings within the same class (§8) in the same package (§7) represent references to the same String object (§4.3.1). • Literal strings within different classes in the same package represent references to the same String object. • Literal strings within different classes in different packages likewise represent references to the same String object. • Strings computed by constant expressions (§15.28) are computed at compile time and then treated as if they were literals. • Strings computed by concatenation at run-time are newly created and therefore distinct. • The result of explicitly interning a computed string is the same string as any pre-existing literal string with the same contents.--------------------------------------------------------------------------------------------------------------------------------------扩展六(Escape Sequences for Character and String Literals): ------------------------------------------------------------------------------------------------------------------------------------- 说明: 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 (§3.10.4) and string literals (§3.10.5). It is a compile-time error if the character following a backslash in an escape is not an ASCIIb,t,n,f,r,",‘,\,0,1,2,3,4,5,6, or7. The Unicode escape\u is processed earlier (§3.3). Octal escapes are provided for compatibility with C, but can express only Unicode values \u0000 through \u00FF, so Unicode escapes are usually preferred. ----------------------------------------------------------------------------------------------------------------------------------- 文法: 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-----------------------------------------------------------------------------------------------------------------------------------------------扩展七(The Null Literal): ------------------------------------------------------------------------------------------------------------------------------ 说明: The null type has one value, the null reference, represented by the null literal null, which is formed from ASCII characters. A null literal is always of the null type. ------------------------------------------------------------------------------------------------------------------------------------- 文法: NullLiteral: null
Java文法(7)—Literals
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。