Literals

In the notes on variable declarations, we saw several examples of variables being given an initial value as soon as the variable was declared. For example, the declaration int x = 23; creates or declares the variable x as an integer and gives it the value 23. In your code, we call the value 23 a literal. More speficically, we call 23 a numeric literal. We also have real literals, e.g., 12.5 or 0.33, character literals such as 'D' and '%', boolean literals which are true and false, and string literals such as "Hi there!" and "D".

By the way, you should note that, although they look similar, the character literal 'D' is not the same as the string literal "D". The first is the character 'D"; the second is the string of one characters "D".

We use literals to put values in our variables, if we know what the values should be when we write the program. If we don't know what the values should be we could leave the variable uninitialized as we did with the declaration int i;. However, when we do this the variable still has a value, we just don't know what it is. We call this unknown value a garbage value, or just garbage. Garbage probably doesn't sound like a good thing to you, and in most cases its not--so in most cases we try to initialize our variables when we declare them.