Java Fundamentals  «Prev


Java Keywords and Literal Values

The Java Language Specification draws a distinction between keywords and literal values.
In Section 3.9 (Keywords), it states that while true and false might appear to be keywords, they are technically Boolean literals. Similarly, while null might appear to be a keyword, it is technically the null literal.
Why does the language specification do this? If it considered the literals true, false, and null as keywords, then it would also have to include the other literal values as keywords. By excluding true, false, null, and the other literal values, it is able to keep the keyword list to a manageable size.
Keywords are words that are used as part of a code structure, like for or while. They change the way a compiler handles a block of code, e.g. a for tells the compiler to execute the code within the specified scope repeatedly,until the given exit condition is reached. The class keyword tells the compiler to treat everything within the specified scope to be part of a particular class. Keyword names are reserved, so you can not use them as variable names.
Literals like true, false and null are values that can be assigned, but their names are reserved in the same way that keywords are, i.e. you cannot have a variable called true or for. They make up parts of expressions.

Literal Values for All Primitive Types

A primitive literal is merely a source code representation of the primitive data types, in other words, an integer, floating-point number, boolean, or character that you type in while writing code. The following are examples of primitive literals:

'b'          // char literal
42         // int literal
false     // boolean literal
89.343 // double literal