[Java] About Java literals

1 minute read

What is a literal?

The notation of a value is called a literal. It is a constant that directly describes a numerical value or a character string, assuming that it will not be changed.
Literal is an English word that means “as it is, as the original meaning of the word”.

There are 6 types of literals in all.

Character literal

The value enclosed in single quotes (‘). It is used when the value is one character. In general, string literals are often used, and character literals are rarely used.
However, use this character literal when using special symbols.

String literal

The value is enclosed in double quotes (“). The value can be one character.

Integer literal

Refers to a value without a decimal point.
You can express 2,8,10, hexadecimal numbers.

Floating point literal

Refers to a value with a decimal point. It can represent decimal numbers and exponents.

Logical value literal

Refers to the value of a logical value.

True = true
False = false

null literal

When referring to reference type data, it means that nothing is referenced.

Example

In the following example, the numeric value 6 is assigned to the int type variable n, and the character string moji is assigned to the String type variable name.
The substituted “6” and “moji” are “numerical literal” and “string literal”, respectively.
Will be.

int n = 6;
String name = moji;

Summary

I understand that literals have no special meaning, they just mean “the characters as they are written”.