Originally posted by Joel McNary: ...A literal value is a value that is hard-coded into the code; it is the opposite of a variable. if you see
you see an if statement comparing the variable a to the literal value 10. null is a literal reference (note that is is not a literal object...), whilte true and false are literal booleans.
Just to expand further, Java allows literals for every primitive data type (i.e. int, long, float, double, etc.) and for Strings. A literal int is just a number:
1, 3, 4, 7, 9
You can also specify literal ints in hexidecimal (base 16) with a leading "0x":
0x0000, 0x1234, 0xFFCE
or octal (base 8) with a leading "0":
0010, 0755, 0213
Note that the first example (0010) is not 10. Since it starts with a 0, it is treated as an octal number that converts to the decimal number 8! If this is confusing, then I suggest you google for more information about number systems and converting between different bases.
If you need to store a number as a long, then you should typically append an int literal with "l" or "L":
The capital version is preferred since it is easy to confuse a lower-case ell with a one.
A double literal is a numbers with decimal places:
If you want to use float instead of double, append "f" or "F":
The simplest type of char literal uses the single quote (') around any character:
'a', '?', ' '
You can also specify char literals using "\u" followed by the Unicode value in hexidecimal:
String literals are similar to char literals except you use double quotes ("):
"Hello, World!", "Mommy!"
I hope this gives you a better idea of what literals are. Please come back if you need any clarifications or any other questions.