literal : Java Glossary
©1996-2017 Roedy Green of Canadian Mind Products
literal is an explicit number or string constant used in Java programs. Here is a list of all the variant forms I have found Java to
int1, -1, hex ints 0x0f28, Unicode hex '\u003f', octal 027, Integer.MAX_VALUE (2,147,483,647), Integer.MIN_VALUE (-2,147,483,648) Beware! A lead 0 on an integer e.g. 007 implies OCTAL. That was a major
design blunder inherited from C, guaranteed to introduce puzzling bugs. Be especially careful when specifying months or days, where you naturally tend to provide a lead 0.
Java version 1.7 or later supports binary constants, e.g. 0b01011111 Java version 1.7 or laterallows you
to break up numbers with underscores to make them easier to proofread, e.g. -2_147_483_648
byte / shortThere is no such thing as a byte or short literal. You would have to write it with a cast e.g. (byte)0xff or (short)-99.
long3L, -99l, 0xf011223344L, Long.MAX_VALUE
(9,223,372,036,854,775,807), Long.MIN_VALUE (-9,223,372,036,854,775,808). Beware! some compilers will quietly chop the high bits from literals
that don’t have a trailing L even when assigning to a long. Java version 1.7 or latersupports binary constants, e.g.
0b01011111LJava version 1.7 or later
allows you to break up numbers with underscores to make them easier to proofread, e.g. -2_147_483_648L
float1.0345F, 1.04E-12f, .0345f, 1.04e-13f, Float.NaN.
double5.6E-120D, 123.4d, 0.1, Double.NaN, Math.PI.
Note floating point literals without the explicit trailing f, F, d or D are considered double. In theory you don’t need a lead 0, e.g. 0.1d may be written .1d, though the Solaris and Symantec compilers seem to require it. With floats and double, the lead zero does
not mean octal
Booleantrue and false.
BooleanTRUE and TRUE.
String s = "abcdefghijklmnopqrst"
There is no speed penalty for the + concatenation. It is done at compile time. String literals can be used anywhere you might use a
String reference, e. g. "abc".charAt(1) is legal. For problematic/awkward/reserved/quotable characters
like embedded ", see escape sequences below.
char'A', enclosed in single quotes, or integer forms e.g.
(char)0x45, '\u003f'. For problematic characters like embedded ', see escape sequences below.
Escape sequencesinside char and String literals include:
' ' space
'\u003f' Unicode hex, (must be exactly 4 digits to give a 16-bit Unicode number ).
Make sure you don’t double the \ e.g "\\u003f" or you will just get the
literal hex chars. This can be a hard bug to find if you are comparing your string against a string containing the fancy character.
\u2007 is Figure Space, a space as wide as a digit, to help in aligning numbers.
'\n' newline, ctrl-J (10, x0A)
'\b' backspace, ctrl-H (8, 0x08)
'\f' formfeed, ctrl-L (12, 0x0C)
'\r' carriage return, ctrl-M (13, 0x0D)
'\t' tab, ctrl-I (9, 0x09)
'\'' single quote (optional inside " "),
'\"' double quote (optional inside ' '),
'\377' octal (must be exactly 3 digits. You can get away with fewer, but then you create an ambiguity if the character following the literal just happens to be
in the range 0..7.). This lets you get at only the 8-bit characters in the range 0..377 octal or 0..255 decimal or 0..255 decimal, which still gives you 16-bit Unicode.
\007 bel, ctrl-G (7, 0x07)
\010 backspace, ctrl-H (8, 0x08)
\013 vt vertical tab, ctrl-K (11, 0x0B)
\032 sub (used in DOS/CPM as eof), ctrl-Z (26, 0x1A)
\033 esc ctrl-^ (27, 0x1B)
There is no Pascalian '#nnn' style way of specifying decimal constants. Just use char c = 123;
The following C forms are not supported: '\a' alert
'\v' vertical tab
'\?' question mark
Use the Unicode \uffff form for printable characters. It won’t work for control characters e.g. \u000a because of the rather
flat-footed way \u is treated as if it were a pre-processor macro. It literally embeds the corresponding character into your Java source, as if you had typed it.
Usually this creates a syntax error.
Java does not have 32-bit String literals, like C style code points e.g. \U0001d504. Note the capital U vs the usual \ud504 I wrote the SurrogatePair applet to convert C-style code points to a arcane surrogate pairs to let you use
32-bit Unicode glyphs in your programs.