Literals are pieces of data that are explicitly used. A literal string, for example, might be used for the label for a field. Literals are used when a static (as in unchanging) piece of data is needed. Say we need to calculate something based on days, not weeks. We could use a variable and give it a value of 7. We could also use a literal value of 7 in the calculation.
Use literals when the value is used once for strings. For numeric values (like 7) we might use the literal multiple times. The key with numerics is whether the value of 7 by itself is clear enough to convey its purpose. Using numerics without a clear source for the value is often referred to as “magic numbers”. This is a bad thing. If you use numerics in a manner that is unclear, at the very least, document each value so anyone reading it can determine each number’s purpose.
The following examples are to show good and bad uses of literals.