Java Keywords and Identifiers

Java Keywords

Keywords are predefined, reserved words used in Java programming that have special meanings to the compiler. For example:

int score;

Here, int is a keyword. It indicates that the variable score is of integer type (32-bit signed two's complement integer).

You cannot use keywords like int, for, class, etc as variable name (or identifiers) as they are part of the Java programming language syntax. Here's the complete list of all keywords in Java programming.

Java Keywords List
abstract assert boolean break byte
case catch char class const
continue default do double else
enum extends final finally float
for goto if implements import
instanceof int interface long native
new package private protected public
return short static strictfp super
switch synchronized this throw throws
transient try void volatile while

Beside these keywords, you cannot also use true, false and null as identifiers. It is because they are literals. To learn more about literals, visit Java literals.


Java identifiers

Identifiers are the name given to variables, classes, methods, etc. Consider the above code;

int score;

Here, score is a variable (an identifier). You cannot use keywords as variable names. It's because keywords have predefined meanings. For example,

int float;

The above code is wrong. It's because float is a keyword and cannot be used as a variable name.

To learn more about variables, visit Java variables.


Rules for Naming an Identifier

  • Identifiers cannot be a keyword.
  • Identifiers are case-sensitive.
  • It can have a sequence of letters and digits. However, it must begin with a letter, $ or _. The first letter of an identifier cannot be a digit.
  • It's a convention to start an identifier with a letter rather and $ or _.
  • Whitespaces are not allowed.
  • Similarly, you cannot use symbols such as @, #, and so on.

Here are some valid identifiers:

  • score
  • level
  • highestScore
  • number1
  • convertToString

Here are some invalid identifiers:

  • class
  • float
  • 1number
  • highest Score
  • @pple