Skip to content

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, 2 is 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, s is 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.

  • int literals do not contain a decimal point: 42, 0, -123, 1928374.
  • double literals do contain a decimal point: 0.0, 3.14, -15.9997.
  • char literals are always enclosed in a pair of single quotes: 'H', 'k', '9', '(', '*'.
  • boolean literals are either of the Java keywords true or false.
  • String literals 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.


Prev -- Up -- Next