Java Keywords and Reserved Words

Java Keywords are the words that are used by Java for internal use. Let’s say we declare a variable:

int id = 1234;

Java will allocate 4 bytes of memory to the variable id. This is because int is already recognized by Java and it knows what to do when a developer uses int word and also knows how much memory to allocate for its variable like id. If the word int was not already recognized by Java then it would have done nothing.

Let’s take another example:

for(int a=1; a<=5; a++){
    System.out.println(a);
}

Java knows to execute print statement 5 times because it already knows what to do when the developer defines the word for.

Hence, the keywords are the predefined words in Java that have internal functionality and Java does internal processing based on these keywords.

Following are the list of keywords in Java.

abstractcontinuefornewswitch
assertdefaultgotopackagesynchronized
booleandoifprivatethis
breakdoubleimplementsprotectedthrow
byteelseimportpublicthrows
caseenuminstanceofreturntransient
catchextendsintshorttry
charfinalinterfacestaticvoid
classfinallylongstrictfpvolatile
constfloatnativesuperwhile

The following keywords are not used by Java yet but are reserved and we can’t use them as an identifier in our program.

  • const
  • goto
  • strictfp

There are three literals in Java Keywords and these are:

  • true
  • false
  • null

Frequently Asked Questions

What are keywords in Java?

Keywords in Java are reserved words that have predefined meanings and cannot be used as identifiers (e.g., variable names, class names).

How many keywords are there in Java?

Java has around 56 keywords that are recognized by the Java compiler, each serving a specific purpose in the language. Detail can be read on List of Java Keywords.

Can I use keywords as variable names or identifiers in Java?

No, you cannot use keywords as variable names or identifiers in Java. Attempting to do so will result in a compilation error.

What are reserved words in Java?

Reserved words are keywords that are not currently used by the language but are reserved for potential future use. They should not be used as identifiers to avoid conflicts with future versions of Java.

Why is it important to be aware of Java keywords and reserved words?

Understanding Java keywords and reserved words is crucial for writing error-free and maintainable code, as using them improperly can lead to compilation errors and unexpected behavior.

Reference: Java Language Keywords