Java Literals

Literal

A literal means the data itself. It refers to unchanging data placed into a variable.

Look at the example below.

int a = 1;

a after int is a variable, and 1 here is a literal.
In other words, unchanging data such as 1(boolean, char, double, long, int, etc.) is called a literal.

Integer

This is the most commonly used data type. Every arbitrary integer value is an integer literal. For example, 1, 2, 3, and 42 are integer literals. These values are decimal, but octal or hexadecimal can also be used.
Octal values are written by placing the digit 0 before the number. Since normal decimal values do not display 0, 07 and 7 are recognized differently.
Hexadecimal values are written by placing 0x(or 0X) first and then specifying the hexadecimal constant. The range is 0 to 16, and a to f(A to F) represent 10 to 15.
Because all integer data is basically int type, to specify an exact long literal for the long data type, add the alphabet l(or L) after the number.
When storing numbers in byte and short variables, no error occurs if the stored number is within the range of that data type.

Floating Point

Floating-point literals are decimal values with a fractional part after the decimal point. For example, 2.0, 3.1415, and -0.6667 are all floating-point literals.
The double type is the default type for floating points. You may also add the alphabet d(or D) after the number.
The float type is a floating-point type. You may add the alphabet f(or F) after the number.

Boolean

Boolean constants have only two logical values: true and false. Unlike C/C++, true and false cannot be represented as numbers.

Character

All Java characters use Unicode. They can be converted to integers, and operations such as addition and subtraction are also possible. To display a character literal that is not an integer form, use single quotation marks(', '). For Unicode or characters that cannot be entered directly, use a backslash( \ ) to represent them.

Escape Sequence Description
\ddd Octal character(ddd)
\uxxxx Hexadecimal Unicode character(xxxx)
\` Single quotation mark
\" Double quotation mark
\ Backslash
\r Carriage return
\n New line(or line feed)
\f Form feed
\t Tab
\b Backspace

String

String literals are specified with double quotation marks(", “).

String str = "안녕하세요.";