(window.webpackJsonp=window.webpackJsonp||[]).push([[1621],{2029:function(e,a,t){"use strict";t.r(a);var s=t(31),r=Object(s.a)({},(function(){var e=this,a=e.$createElement,t=e._self._c||a;return t("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[t("h1",{attrs:{id:"literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#literals"}},[e._v("#")]),e._v(" Literals")]),e._v(" "),t("h2",{attrs:{id:"using-underscore-to-improve-readability"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#using-underscore-to-improve-readability"}},[e._v("#")]),e._v(" Using underscore to improve readability")]),e._v(" "),t("p",[e._v("Since Java 7 it has been possible to use one or more underscores (_) for separating groups of digits in a primitive number literal to improve their readability.")]),e._v(" "),t("p",[e._v("For instance, these two declarations are equivalent:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" i1 "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("123456")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" i2 "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("123_456")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token class-name"}},[e._v("System")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),e._v("out"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(".")]),t("span",{pre:!0,attrs:{class:"token function"}},[e._v("println")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v("(")]),e._v("i1 "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("==")]),e._v(" i2"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(")")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// true")]),e._v("\n\n")])])]),t("p",[e._v("This can be applied to all primitive number literals as shown below:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("byte")]),e._v(" color "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("1_2_3")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("short")]),e._v(" yearsAnnoDomini"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("2_016")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" socialSecurtyNumber "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("999_99_9999")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("long")]),e._v(" creditCardNumber "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("1234_5678_9012_3456L")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("float")]),e._v(" piFourDecimals "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("3.14_15F")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("double")]),e._v(" piTenDecimals "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("3.14_15_92_65_35")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n\n")])])]),t("p",[e._v("This also works using prefixes for binary, octal and hexadecimal bases:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("short")]),e._v(" binary"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("0b0_1_0_1")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" octal "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("07_7_7_7_7_7_7_7_0")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("long")]),e._v(" hexBytes "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("0xFF_EC_DE_5E")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n\n")])])]),t("p",[e._v("There are a few rules about underscores which "),t("strong",[e._v("forbid")]),e._v(" their placement in the following places:")]),e._v(" "),t("ul",[t("li",[e._v("At the beginning or end of a number (e.g. "),t("code",[e._v("_123")]),e._v(" or "),t("code",[e._v("123_")]),e._v(" are "),t("strong",[e._v("not")]),e._v(" valid)")]),e._v(" "),t("li",[e._v("Adjacent to a decimal point in a floating point literal (e.g. "),t("code",[e._v("1._23")]),e._v(" or "),t("code",[e._v("1_.23")]),e._v(" are "),t("strong",[e._v("not")]),e._v(" valid)")]),e._v(" "),t("li",[e._v("Prior to an F or L suffix (e.g. "),t("code",[e._v("1.23_F")]),e._v(" or "),t("code",[e._v("9999999_L")]),e._v(" are "),t("strong",[e._v("not")]),e._v(" valid)")]),e._v(" "),t("li",[e._v("In positions where a string of digits is expected (e.g. "),t("code",[e._v("0_xFFFF")]),e._v(" is "),t("strong",[e._v("not")]),e._v(" valid)")])]),e._v(" "),t("h2",{attrs:{id:"hexadecimal-octal-and-binary-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#hexadecimal-octal-and-binary-literals"}},[e._v("#")]),e._v(" Hexadecimal, Octal and Binary literals")]),e._v(" "),t("p",[e._v("A "),t("code",[e._v("hexadecimal")]),e._v(" number is a value in base-16.\nThere are 16 digits, "),t("code",[e._v("0-9")]),e._v(" and the letters "),t("code",[e._v("A-F")]),e._v(" (case does not matter).\n"),t("code",[e._v("A-F")]),e._v(" represent "),t("code",[e._v("10-16")]),e._v(".")]),e._v(" "),t("p",[e._v("An "),t("code",[e._v("octal")]),e._v(" number is a value in base-8, and uses the digits "),t("code",[e._v("0-7")]),e._v(".")]),e._v(" "),t("p",[e._v("A "),t("code",[e._v("binary")]),e._v(" number is a value in base-2, and uses the digits "),t("code",[e._v("0")]),e._v(" and "),t("code",[e._v("1")]),e._v(".")]),e._v(" "),t("p",[e._v("All of these numbers result in the same value, "),t("code",[e._v("110")]),e._v(":")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" dec "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("110")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// no prefix --\x3e decimal literal")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" bin "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("0b1101110")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// '0b' prefix --\x3e binary literal")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" oct "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("0156")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// '0' prefix --\x3e octal literal")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" hex "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("0x6E")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// '0x' prefix --\x3e hexadecimal literal")]),e._v("\n\n")])])]),t("p",[e._v("Note that binary literal syntax was introduced in Java 7.")]),e._v(" "),t("p",[e._v("The octal literal can easily be a trap for semantic errors. If you define a leading "),t("code",[e._v("'0'")]),e._v(" to your decimal literals you will get the wrong value:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("int")]),e._v(" a "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("0100")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// Instead of 100, a == 64")]),e._v("\n\n")])])]),t("h2",{attrs:{id:"boolean-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#boolean-literals"}},[e._v("#")]),e._v(" Boolean literals")]),e._v(" "),t("p",[e._v("Boolean literals are the simplest of the literals in the Java programming language. The two possible "),t("code",[e._v("boolean")]),e._v(" values are represented by the literals "),t("code",[e._v("true")]),e._v(" and "),t("code",[e._v("false")]),e._v(". These are case-sensitive. For example:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("boolean")]),e._v(" flag "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token boolean"}},[e._v("true")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// using the 'true' literal")]),e._v("\nflag "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token boolean"}},[e._v("false")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// using the 'false' literal")]),e._v("\n\n")])])]),t("h2",{attrs:{id:"escape-sequences-in-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#escape-sequences-in-literals"}},[e._v("#")]),e._v(" Escape sequences in literals")]),e._v(" "),t("p",[e._v("String and character literals provide an escape mechanism that allows express character codes that would otherwise not be allowed in the literal. An escape sequence consists of a backslash character ("),t("code",[e._v("\\")]),e._v(") followed by one ore more other characters. The same sequences are valid in both character an string literals.")]),e._v(" "),t("p",[e._v("The complete set of escape sequences is as follows:")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("Escape sequence")]),e._v(" "),t("th",[e._v("Meaning")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[t("code",[e._v("\\\\")])]),e._v(" "),t("td",[e._v("Denotes an backslash ("),t("code",[e._v("\\")]),e._v(") character")])]),e._v(" "),t("tr",[t("td",[t("code",[e._v("\\'")])]),e._v(" "),t("td",[e._v("Denotes a single-quote ("),t("code",[e._v("'")]),e._v(") character")])]),e._v(" "),t("tr",[t("td",[t("code",[e._v('\\"')])]),e._v(" "),t("td",[e._v("Denotes a double-quote ("),t("code",[e._v('"')]),e._v(") character")])]),e._v(" "),t("tr",[t("td",[t("code",[e._v("\\n")])]),e._v(" "),t("td",[e._v("Denotes a line feed ("),t("code",[e._v("LF")]),e._v(") character")])]),e._v(" "),t("tr",[t("td",[t("code",[e._v("\\r")])]),e._v(" "),t("td",[e._v("Denotes a carriage return ("),t("code",[e._v("CR")]),e._v(") character")])]),e._v(" "),t("tr",[t("td",[t("code",[e._v("\\t")])]),e._v(" "),t("td",[e._v("Denotes a horizontal tab ("),t("code",[e._v("HT")]),e._v(") character")])]),e._v(" "),t("tr",[t("td",[t("code",[e._v("\\f")])]),e._v(" "),t("td",[e._v("Denotes a form feed ("),t("code",[e._v("FF")]),e._v(") character")])]),e._v(" "),t("tr",[t("td",[t("code",[e._v("\\b")])]),e._v(" "),t("td",[e._v("Denotes a backspace ("),t("code",[e._v("BS")]),e._v(") character")])]),e._v(" "),t("tr",[t("td",[t("code",[e._v("\\")])]),e._v(" "),t("td",[e._v("Denotes a character code in the range 0 to 255.")])])])]),e._v(" "),t("p",[e._v("The "),t("code",[e._v("")]),e._v(" in the above consists of one, two or three octal digits ('0' through '7') which represent a number between 0 and 255 (decimal).")]),e._v(" "),t("p",[e._v("Note that a backslash followed by any other character is an invalid escape sequence. Invalid escape sequences are treated as compilation errors by the JLS.")]),e._v(" "),t("p",[e._v("Reference:")]),e._v(" "),t("ul",[t("li",[t("a",{attrs:{href:"https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.6",target:"_blank",rel:"noopener noreferrer"}},[e._v("JLS 3.10.6. Escape Sequences for Character and String Literals"),t("OutboundLink")],1)])]),e._v(" "),t("h3",{attrs:{id:"unicode-escapes"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#unicode-escapes"}},[e._v("#")]),e._v(" Unicode escapes")]),e._v(" "),t("p",[e._v("In addition to the string and character escape sequences described above, Java has a more general Unicode escaping mechanism, as defined in "),t("a",{attrs:{href:"https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.3",target:"_blank",rel:"noopener noreferrer"}},[e._v("JLS 3.3. Unicode Escapes"),t("OutboundLink")],1),e._v(". A Unicode escape has the following syntax:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token string"}},[e._v("'\\' '")]),e._v("u' "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("<")]),e._v("hex"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("-")]),e._v("digit"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v(">")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("<")]),e._v("hex"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("-")]),e._v("digit"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v(">")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("<")]),e._v("hex"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("-")]),e._v("digit"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v(">")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("<")]),e._v("hex"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("-")]),e._v("digit"),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v(">")]),e._v(" \n\n")])])]),t("p",[e._v("where "),t("code",[e._v("")]),e._v(" is one of "),t("code",[e._v("'0'")]),e._v(", "),t("code",[e._v("'1'")]),e._v(", "),t("code",[e._v("'2'")]),e._v(", "),t("code",[e._v("'3'")]),e._v(", "),t("code",[e._v("'4'")]),e._v(", "),t("code",[e._v("'5'")]),e._v(", "),t("code",[e._v("'6'")]),e._v(", "),t("code",[e._v("'7'")]),e._v(", "),t("code",[e._v("'8'")]),e._v(", "),t("code",[e._v("'9'")]),e._v(", "),t("code",[e._v("'a'")]),e._v(", "),t("code",[e._v("'b'")]),e._v(", "),t("code",[e._v("'c'")]),e._v(", "),t("code",[e._v("'d'")]),e._v(", "),t("code",[e._v("'e'")]),e._v(", "),t("code",[e._v("'f'")]),e._v(", "),t("code",[e._v("'A'")]),e._v(", "),t("code",[e._v("'B'")]),e._v(", "),t("code",[e._v("'C'")]),e._v(", "),t("code",[e._v("'D'")]),e._v(", "),t("code",[e._v("'E'")]),e._v(", "),t("code",[e._v("'F'")]),e._v(".")]),e._v(" "),t("p",[e._v("A Unicode escape is mapped by the Java compiler to a character (strictly speaking a 16-bit Unicode "),t("strong",[e._v("code unit")]),e._v("), and can be used anywhere in the source code where the mapped character is valid. It is commonly used in character and string literals when you need to represent a non-ASCII character in a literal.")]),e._v(" "),t("h3",{attrs:{id:"escaping-in-regexes"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#escaping-in-regexes"}},[e._v("#")]),e._v(" Escaping in regexes")]),e._v(" "),t("p",[e._v("TBD")]),e._v(" "),t("h2",{attrs:{id:"decimal-integer-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#decimal-integer-literals"}},[e._v("#")]),e._v(" Decimal Integer literals")]),e._v(" "),t("p",[e._v("Integer literals provide values that can be used where you need a "),t("code",[e._v("byte")]),e._v(", "),t("code",[e._v("short")]),e._v(", "),t("code",[e._v("int")]),e._v(", "),t("code",[e._v("long")]),e._v(" or "),t("code",[e._v("char")]),e._v(" instance. (This example focuses on the simple decimal forms. Other examples explain how to literals in octal, hexadecimal and binary, and the use of underscores to improve readability.)")]),e._v(" "),t("h3",{attrs:{id:"ordinary-integer-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#ordinary-integer-literals"}},[e._v("#")]),e._v(" Ordinary integer literals")]),e._v(" "),t("p",[e._v("The simplest and most common form of integer literal is a decimal integer literal. For example:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n0 // The decimal number zero (type 'int')\n 1 // The decimal number one (type 'int')\n 42 // The decimal number forty two (type 'int')\n\n")])])]),t("p",[e._v("You need to be careful with leading zeros. A leading zero causes an integer literal to be interpreted as "),t("strong",[e._v("octal")]),e._v(" not decimal.")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n077 // This literal actually means 7 x 8 + 7 ... or 63 decimal!\n\n")])])]),t("p",[e._v("Integer literals are unsigned. If you see something like "),t("code",[e._v("-10")]),e._v(" or "),t("code",[e._v("+10")]),e._v(", these are actually "),t("strong",[e._v("expressions")]),e._v(" using the unary "),t("code",[e._v("-")]),e._v(" and unary "),t("code",[e._v("+")]),e._v(" operators.")]),e._v(" "),t("p",[e._v("The range of integer literals of this form have an intrinsic type of "),t("code",[e._v("int")]),e._v(", and must fall in the range zero to 2"),t("sup",[e._v("31")]),e._v(" or 2,147,483,648.")]),e._v(" "),t("p",[e._v("Note that 2"),t("sup",[e._v("31")]),e._v(" is "),t("code",[e._v("1")]),e._v(" greater than "),t("code",[e._v("Integer.MAX_VALUE")]),e._v(". Literals from 0 through to "),t("code",[e._v("2147483647")]),e._v(" can be used anywhere, but it is a compilation error to use "),t("code",[e._v("2147483648")]),e._v(" without a preceding unary "),t("code",[e._v("-")]),e._v(" operator. (In other words, it is reserved for expressing the value of "),t("code",[e._v("Integer.MIN_VALUE")]),e._v(".)")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\nint max = 2147483647; // OK\n int min = -2147483648; // OK\n int tooBig = 2147483648; // ERROR\n\n")])])]),t("h3",{attrs:{id:"long-integer-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#long-integer-literals"}},[e._v("#")]),e._v(" Long integer literals")]),e._v(" "),t("p",[e._v("Literals of type "),t("code",[e._v("long")]),e._v(" are expressed by adding an "),t("code",[e._v("L")]),e._v(" suffix. For example:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n0L // The decimal number zero (type 'long')\n 1L // The decimal number one (type 'long')\n 2147483648L // The value of Integer.MAX_VALUE + 1\n\n long big = 2147483648; // ERROR\n long big2 = 2147483648L; // OK\n\n")])])]),t("p",[e._v("Note that the distinction between "),t("code",[e._v("int")]),e._v(" and "),t("code",[e._v("long")]),e._v(" literals is significant in other places. For example")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\nint i = 2147483647;\n long l = i + 1; // Produces a negative value because the operation is\n // performed using 32 bit arithmetic, and the \n // addition overflows\n long l2 = i + 1L; // Produces the (intuitively) correct value.\n\n")])])]),t("p",[e._v("Reference: "),t("a",{attrs:{href:"https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.1",target:"_blank",rel:"noopener noreferrer"}},[e._v("JLS 3.10.1 - Integer Literals"),t("OutboundLink")],1)]),e._v(" "),t("h2",{attrs:{id:"string-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#string-literals"}},[e._v("#")]),e._v(" String literals")]),e._v(" "),t("p",[e._v("String literals provide the most convenient way to represent string values in Java source code. A String literal consists of:")]),e._v(" "),t("ul",[t("li",[e._v("An opening double-quote ("),t("code",[e._v('"')]),e._v(") character.")]),e._v(" "),t("li",[e._v("Zero or more other characters that are neither a double-quote or a line-break character. (A backslash ("),t("code",[e._v("\\")]),e._v(") character alters the meaning of subsequent characters; see "),t("a",{attrs:{href:"http://stackoverflow.com/documentation/java/8250/literals/26505/escape-sequences-in-literals#t=20161220000946886904",target:"_blank",rel:"noopener noreferrer"}},[e._v("Escape sequences in literals"),t("OutboundLink")],1),e._v(".)")]),e._v(" "),t("li",[e._v("A closing double-quote character.")])]),e._v(" "),t("p",[e._v("For example:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token string"}},[e._v('"Hello world"')]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// A literal denoting an 11 character String")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token string"}},[e._v('""')]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// A literal denoting an empty (zero length) String")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token string"}},[e._v('"\\""')]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// A literal denoting a String consisting of one ")]),e._v("\n "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// double quote character")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token string"}},[e._v('"1\\t2\\t3\\n"')]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// Another literal with escape sequences")]),e._v("\n\n")])])]),t("p",[e._v("Note that a single string literal may not span multiple source code lines. It is a compilation error for a line-break (or the end of the source file) to occur before a literal's closing double-quote. For example:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[e._v('"'),t("span",{pre:!0,attrs:{class:"token class-name"}},[e._v("Jello")]),e._v(" world "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// Compilation error (at the end of the line!)")]),e._v("\n\n")])])]),t("h3",{attrs:{id:"long-strings"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#long-strings"}},[e._v("#")]),e._v(" Long strings")]),e._v(" "),t("p",[e._v("If you need a string that is too long to fit on a line, the conventional way to express it is to split it into multiple literals and use the concatenation operator ("),t("code",[e._v("+")]),e._v(") to join the pieces. For example")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token class-name"}},[e._v("String")]),e._v(" typingPractice "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[e._v('"The quick brown fox "')]),e._v(" "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("+")]),e._v("\n "),t("span",{pre:!0,attrs:{class:"token string"}},[e._v('"jumped over "')]),e._v(" "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("+")]),e._v("\n "),t("span",{pre:!0,attrs:{class:"token string"}},[e._v('"the lazy dog"')]),e._v("\n\n")])])]),t("p",[e._v("An expression like the above consisting of string literals and "),t("code",[e._v("+")]),e._v(" satisfies the requirements to be a "),t("a",{attrs:{href:"http://stackoverflow.com/documentation/java/8167/expressions/26261/constant-expressions#t=201612261246273998187",target:"_blank",rel:"noopener noreferrer"}},[e._v("Constant Expression"),t("OutboundLink")],1),e._v(". That means that the expression will be evaluated by the compiler and represented at runtime by a single "),t("code",[e._v("String")]),e._v(" object.")]),e._v(" "),t("h3",{attrs:{id:"interning-of-string-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#interning-of-string-literals"}},[e._v("#")]),e._v(" Interning of string literals")]),e._v(" "),t("p",[e._v("When class file containing string literals is loaded by the JVM, the corresponding "),t("code",[e._v("String")]),e._v(" objects are "),t("strong",[e._v("interned")]),e._v(" by the runtime system. This means that a string literal used in multiple classes occupies no more space than if it was used in one class.")]),e._v(" "),t("p",[e._v("For more information on interning and the string pool, refer to the "),t("a",{attrs:{href:"http://stackoverflow.com/documentation/java/109/strings/10577/string-pool-and-heap-storage#t=201612200451213663069",target:"_blank",rel:"noopener noreferrer"}},[e._v("String pool and heap storage"),t("OutboundLink")],1),e._v(" example in the Strings topic.")]),e._v(" "),t("h2",{attrs:{id:"the-null-literal"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#the-null-literal"}},[e._v("#")]),e._v(" The Null literal")]),e._v(" "),t("p",[e._v("The Null literal (written as "),t("code",[e._v("null")]),e._v(") represents the one and only value of the null type. Here are some examples")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n MyClass object = null;\n MyClass[] objects = new MyClass[]{new MyClass(), null, new MyClass()};\n\n myMethod(null);\n\n if (objects != null) {\n // Do something\n }\n\n")])])]),t("p",[e._v("The null type is rather unusual. It has no name, so you cannot express it in Java source code. (And it has no runtime representation either.)")]),e._v(" "),t("p",[e._v("The sole purpose of the null type is to be the type of "),t("code",[e._v("null")]),e._v(". It is assignment compatible with all reference types, and can be type cast to any reference type. (In the latter case, the cast does not entail a runtime type check.)")]),e._v(" "),t("p",[e._v("Finally, "),t("code",[e._v("null")]),e._v(" has the property that "),t("code",[e._v("null instanceof ")]),e._v(" will evaluate to "),t("code",[e._v("false")]),e._v(", no matter what the type is.")]),e._v(" "),t("h2",{attrs:{id:"floating-point-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#floating-point-literals"}},[e._v("#")]),e._v(" Floating-point literals")]),e._v(" "),t("p",[e._v("Floating point literals provide values that can be used where you need a "),t("code",[e._v("float")]),e._v(" or "),t("code",[e._v("double")]),e._v(" instance. There are three kinds of floating point literal.")]),e._v(" "),t("ul",[t("li",[e._v("Simple decimal forms")]),e._v(" "),t("li",[e._v("Scaled decimal forms")]),e._v(" "),t("li",[e._v("Hexadecimal forms")])]),e._v(" "),t("p",[e._v("(The JLS syntax rules combine the two decimal forms into a single form. We treat them separately for ease of explanation.)")]),e._v(" "),t("p",[e._v("There are distinct literal types for "),t("code",[e._v("float")]),e._v(" and "),t("code",[e._v("double")]),e._v(" literals, expressed using suffixes. The various forms use letters to express different things. These letters are case insensitive.")]),e._v(" "),t("h3",{attrs:{id:"simple-decimal-forms"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#simple-decimal-forms"}},[e._v("#")]),e._v(" Simple decimal forms")]),e._v(" "),t("p",[e._v("The simplest form of floating point literal consists of one or more decimal digits and a decimal point ("),t("code",[e._v(".")]),e._v(") and an optional suffix ("),t("code",[e._v("f")]),e._v(", "),t("code",[e._v("F")]),e._v(", "),t("code",[e._v("d")]),e._v(" or "),t("code",[e._v("D")]),e._v("). The optional suffix allows you to specify that the literal is a "),t("code",[e._v("float")]),e._v(" ("),t("code",[e._v("f")]),e._v(" or "),t("code",[e._v("F")]),e._v(") or "),t("code",[e._v("double")]),e._v(" ("),t("code",[e._v("d")]),e._v(" or "),t("code",[e._v("D")]),e._v(") value. The default (when no suffix is specified) is "),t("code",[e._v("double")]),e._v(".")]),e._v(" "),t("p",[e._v("For example")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n0.0 // this denotes zero\n .0 // this also denotes zero\n 0. // this also denotes zero\n 3.14159 // this denotes Pi, accurate to (approximately!) 5 decimal places.\n 1.0F // a `float` literal\n 1.0D // a `double` literal. (`double` is the default if no suffix is given)\n\n")])])]),t("p",[e._v("In fact, decimal digits followed by a suffix is also a floating point literal.")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n1F // means the same thing as 1.0F\n\n")])])]),t("p",[e._v("The meaning of a decimal literal is the IEEE floating point number that is "),t("strong",[e._v("closest")]),e._v(" to the infinite precision mathematical Real number denoted by the decimal floating point form. This conceptual value is converted to IEEE binary floating point representation using "),t("strong",[e._v("round to nearest")]),e._v(". (The precise semantics of decimal conversion are specified in the javadocs for "),t("a",{attrs:{href:"http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#valueOf-java.lang.String-",target:"_blank",rel:"noopener noreferrer"}},[t("code",[e._v("Double.valueOf(String)")]),t("OutboundLink")],1),e._v(" and "),t("a",{attrs:{href:"http://docs.oracle.com/javase/8/docs/api/java/lang/Float.html#valueOf-java.lang.String-",target:"_blank",rel:"noopener noreferrer"}},[t("code",[e._v("Float.valueOf(String)")]),t("OutboundLink")],1),e._v(", bearing in mind that there are differences in the number syntaxes.)")]),e._v(" "),t("h3",{attrs:{id:"scaled-decimal-forms"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#scaled-decimal-forms"}},[e._v("#")]),e._v(" Scaled decimal forms")]),e._v(" "),t("p",[e._v("Scaled decimal forms consist of simple decimal with an exponent part introduced by an "),t("code",[e._v("E")]),e._v(" or "),t("code",[e._v("e")]),e._v(", and followed by a signed integer. The exponent part is a short hand for multiplying the decimal form by a power of ten, as shown in the examples below. There is also an optional suffix to distinguish "),t("code",[e._v("float")]),e._v(" and "),t("code",[e._v("double")]),e._v(" literals. Here are some examples:")]),e._v(" "),t("div",{staticClass:"language- extra-class"},[t("pre",{pre:!0,attrs:{class:"language-text"}},[t("code",[e._v("\n1.0E1 // this means 1.0 x 10^1 ... or 10.0 (double)\n 1E-1D // this means 1.0 x 10^(-1) ... or 0.1 (double)\n 1.0e10f // this means 1.0 x 10^(10) ... or 10000000000.0 (float)\n\n")])])]),t("p",[e._v("The size of a literal is limited by the representation ("),t("code",[e._v("float")]),e._v(" or "),t("code",[e._v("double")]),e._v("). It is a compilation error if the scale factor results in a value that is too large or too small.")]),e._v(" "),t("h3",{attrs:{id:"hexadecimal-forms"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#hexadecimal-forms"}},[e._v("#")]),e._v(" Hexadecimal forms")]),e._v(" "),t("p",[e._v("Starting with Java 6, it is possible to express floating point literals in hexadecimal. The hexadecimal form have an analogous syntax to the simple and scaled decimal forms with the following differences:")]),e._v(" "),t("ol",[t("li",[e._v("Every hexadecimal floating point literal starts with a zero ("),t("code",[e._v("0")]),e._v(") and then an "),t("code",[e._v("x")]),e._v(" or "),t("code",[e._v("X")]),e._v(".")]),e._v(" "),t("li",[e._v("The digits of the number (but "),t("strong",[e._v("not")]),e._v(" the exponent part!) also include the hexadecimal digits "),t("code",[e._v("a")]),e._v(" through "),t("code",[e._v("f")]),e._v(" and their uppercase equivalents.")]),e._v(" "),t("li",[e._v("The exponent is "),t("strong",[e._v("mandatory")]),e._v(", and is introduced by the letter "),t("code",[e._v("p")]),e._v(" (or "),t("code",[e._v("P")]),e._v(") instead of an "),t("code",[e._v("e")]),e._v(" or "),t("code",[e._v("E")]),e._v(". The exponent represents a scaling factor that is a power of 2 instead of a power of 10.")])]),e._v(" "),t("p",[e._v("Here are some examples:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token number"}},[e._v("0x0.0p0f")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// this is zero expressed in hexadecimal form (`float`)")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token number"}},[e._v("0xff.0p19")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// this is 255.0 x 2^19 (`double`)")]),e._v("\n\n")])])]),t("p",[e._v("Advice: since hexadecimal floating-point forms are unfamiliar to most Java programmers, it is advisable to use them sparingly.")]),e._v(" "),t("h3",{attrs:{id:"underscores"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#underscores"}},[e._v("#")]),e._v(" Underscores")]),e._v(" "),t("p",[e._v('Starting with Java 7, underscores are permitted within the digit strings in all three forms of floating point literal. This applies to the "exponent" parts as well. See '),t("a",{attrs:{href:"http://stackoverflow.com/documentation/java/8250/literals/12632/using-underscore-to-improve-readability#t=201612261252199151525",target:"_blank",rel:"noopener noreferrer"}},[e._v("Using underscores to improve readability"),t("OutboundLink")],1),e._v(".")]),e._v(" "),t("h3",{attrs:{id:"special-cases"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#special-cases"}},[e._v("#")]),e._v(" Special cases")]),e._v(" "),t("p",[e._v("It is a compilation error if a floating point literal denotes a number that is too large or too small to represent in the selected representation; i.e. if the number would overflow to +INF or -INF, or underflow to 0.0. However, it is legal for a literal to represent a non-zero denormalized number.")]),e._v(" "),t("p",[e._v("The floating point literal syntax does not provide literal representations for IEEE 754 special values such as the INF and NaN values. If you need to express them in source code, the recommended way is to use the constants defined by the "),t("code",[e._v("java.lang.Float")]),e._v(" and "),t("code",[e._v("java.lang.Double")]),e._v("; e.g. "),t("code",[e._v("Float.NaN")]),e._v(", "),t("code",[e._v("Float.NEGATIVE_INFINITY")]),e._v(" and "),t("code",[e._v("Float.POSITIVE_INFINITY")]),e._v(".")]),e._v(" "),t("h2",{attrs:{id:"character-literals"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#character-literals"}},[e._v("#")]),e._v(" Character literals")]),e._v(" "),t("p",[e._v("Character literals provide the most convenient way to express "),t("code",[e._v("char")]),e._v(" values in Java source code. A character literal consists of:")]),e._v(" "),t("ul",[t("li",[e._v("An opening single-quote ("),t("code",[e._v("'")]),e._v(") character.")]),e._v(" "),t("li",[e._v("A representation of a character. This representation cannot be a single-quote or a line-break character, but it can be an escape sequence introduced by a backslash ("),t("code",[e._v("\\")]),e._v(") character; see "),t("a",{attrs:{href:"http://stackoverflow.com/documentation/java/8250/literals/26505/escape-sequences-in-literals#t=20161220000946886904",target:"_blank",rel:"noopener noreferrer"}},[e._v("Escape sequences in literals"),t("OutboundLink")],1),e._v(".")]),e._v(" "),t("li",[e._v("A closing single-quote ("),t("code",[e._v("'")]),e._v(") character.")])]),e._v(" "),t("p",[e._v("For example:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("char")]),e._v(" a "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[e._v("'a'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("char")]),e._v(" doubleQuote "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[e._v("'\"'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("char")]),e._v(" singleQuote "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[e._v("'\\''")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v("\n\n")])])]),t("p",[e._v("A line-break in a character literal is a compilation error:")]),e._v(" "),t("div",{staticClass:"language-java extra-class"},[t("pre",{pre:!0,attrs:{class:"language-java"}},[t("code",[t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("char")]),e._v(" newline "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" '\n"),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// Compilation error in previous line")]),e._v("\n"),t("span",{pre:!0,attrs:{class:"token keyword"}},[e._v("char")]),e._v(" newLine "),t("span",{pre:!0,attrs:{class:"token operator"}},[e._v("=")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token string"}},[e._v("'\\n'")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[e._v(";")]),e._v(" "),t("span",{pre:!0,attrs:{class:"token comment"}},[e._v("// Correct")]),e._v("\n\n")])])])])}),[],!1,null,null,null);a.default=r.exports}}]);