Literals

In addition to variables, you will also use a literal in a Java statement.

A literal is any number, text, or other information that directly represents a value.

Literal is a programming term that essentially means that what you type is what you get. The following assignment statement uses a literal:

int year = 2000;

The literal is 2000, because it directly represents the integer value 2000. Numbers, characters, and strings all are examples of literals.

Although the meaning and usage of literals will seem intuitive most of the time, Java has some special types of literals that represent different kinds of numbers, characters, strings, and Boolean values.

Number Literals

Java has several integer literals. The number 4, for example, ...

Get Sams Teach Yourself Java 2 in 21 Days, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.