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.
| Literal | Kind | Feature |
|---|---|---|
25 | Integer | Written without quotes |
4.5 | Floating-point number | By default, double |
'K' | char | Single quotes |
"Kava" | String | Double quotes |
true | boolean | Only true or false |
null | Null reference | Refers 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.
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.