Literals
Literals are pieces of data whose value cannot change (whereas variables represent those values).
-
If someone tells you to add 2 to a number,
2is a literal value — it has no other name and it represents a fixed amount. -
If someone tells you to put the letter s at the end of a word,
sis a literal value — it has no name and it represents one particular letter.
You can put literal values in your program representing data of any one of the previously mentioned data types.
intliterals do not contain a decimal point:42,0,-123,1928374.doubleliterals do contain a decimal point:0.0,3.14,-15.9997.charliterals are always enclosed in a pair of single quotes:'H','k','9','(','*'.booleanliterals are either of the Java keywordstrueorfalse.Stringliterals are enclosed in double quotes:"Hello world!","dog","Java is a programming language".
Practice Exercise¶
A char literal appears between a pair of single quotes ('), and a String literal appears between a pair of double quotes ("). Getting this wrong is a compiler error.