Java Fundamentals  «Prev  Next»


Lesson 5Java Identifiers and Keywords:
ObjectiveLearn Syntax of Java Identifiers and keywords.

Java Identifiers identifiers and language keywords

Identifiers are used to name elements of Java programs, such as classes, interfaces, variables, methods, constructors, and arguments. A valid name begins with a Unicode letter, underscore character (_), or dollar sign ($). A Unicode letter is an English letter (a-z) or (A-Z) or a letter in a non-English language. The second and remaining letters of an identifier may be a Unicode letter, underscore, dollar sign, or digit (0-9). The dollar sign character is used by compiler-internal identifiers and should be avoided. Identifiers are case sensitive. The identifiers myID and MyID are considered to be separate identifiers. An identifier may not use the literal values, true, false, null, or any of the keywords that are reserved by Java. Keywords may not be used as Java variables.


Java Keywords and Literal Values

The Java Language Specification draws a distinction between keywords and literal values. 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		
    


Number of Keywords in Java SE 22

The Java language reserves a list of 53 keywords.
abstractassertbooleanbreak
bytecasecatchchar
classconstcontinuedefault
dodoubleelseenum
extendsfalsefinalfinally
floatforgotoif
implementsimportinstanceofint
interfacelongnativenew
nullpackageprivateprotected
publicreturnshortstatic
strictfpsuperswitchsynchronized
thisthrowthrowstransient
truetryvoidvolatile
while

In Java SE 22, the total number of keywords remains 53, which is the same number as in Java SE 7. Although new features and functionalities have been introduced in Java since version 7, no new keywords have been added to the language after Java SE 7. However, Java has evolved by adding new constructs and enhancements such as records, pattern matching, and sealed classes, but these have been implemented without the need for introducing additional keywords.

Keyword Identifier - Quiz

Click the Quiz link below to check your understanding of the main() method, identifiers, and keywords.
Keyword Identifier - Quiz

SEMrush Software