Literals
Literal is a value of any type given explicitly. So in expression x+10, x is variable and 10 numeric literal.
Sign + or - can be consider as part of the numeric literal or as expression with unary + and -.
example | description |
---|---|
23 +23 -23 20L |
Decimal integer literal is defined as sequence of digits without a leading 0 (zero). Integer types represents by
|
023 0o23 -023 |
An octal integer literal is defined as sequence of 0-7 digits with leading 0 or prefix 0o. |
0x23 0x23AAbb -0xabcde |
A hexadecimal integer literal is defined as sequence of 0-9 digits and letters a-f or A-F with prefix 0x or 0X. Case of letters is meaningless, so 0xa = 0xA = 10 and 0xf = 0xF = 15 |
0b11 -0b0001 |
A binary integer literal is defined as sequence of 0 and 1 with prefix 0b or 0B. |
1.234e2 -.1415926 123.4f 123.4d |
There are two types for represent floating-point numbers: double
and float. A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d. |
true false |
true and false are boolean literals. |
{1,2,3} { "a", "c", strvar} |
A array literal is defined as sequence of values in curly braces. |
'A' | A character literal is a character in single quotes. May contain any Unicode (UTF-16) character. If your editor and file system allow it, you can use such characters directly in your code. If not, you can use a "Unicode escape": \uxxxx. |
"str\u00F1" "line1\n\rline2" |
A string literal is a sequence of characters in double quotes. May contain any Unicode (UTF-16) characters. If your editor and file system allow it, you can use such characters directly in your code. If not, you can use a "Unicode escape": \uxxxx. |
underscore in numerical literals
Any number of underscore _ characters can appear anywhere between digits in a numerical literal.
long creditCardNumber = 1234_5678_9012_3456L;
int hexBytes = 0xFF_EC_DE_5E;
float pi = 3.14_15F;