OTHER USEFUL INFORMATION

What is a Java literal?

A literal is a value written directly in program code. The number 12, the text "Ieva" and the Boolean value true are examples of literals.

Literals in everyday code

String vardas = "Ieva";
int amzius = 12;
char grupe = 'A';
boolean prisijunge = true;

Here, vardas, amzius, grupe and prisijunge are variable names. The values written to the right of the equals sign are literals.

Common Java literals
LiteralKindFeature
25IntegerWritten without quotes
4.5Floating-point numberBy default, double
'K'charSingle quotes
"Kava"StringDouble quotes
truebooleanOnly true or false
nullNull referenceRefers to no object

Why use suffixes?

long pasaulioGyventoju = 8_100_000_000L;
float temperatura = 21.5f;
double kaina = 3.99;

An integer literal is normally treated as int. When the value is too large for it, the letter L explicitly indicates a long literal. A fractional literal is normally a double, so float values use f.

Choose uppercase L.

Lowercase l can be confused with the digit 1, so the uppercase letter is clearer.

A literal is not the same as a constant

A literal is the value itself written in code. A constant is a named location whose value the program does not allow you to change. For example, in the line final int DIENU = 7; the word DIENU is the constant's name, while 7 is a literal.