Skip to main content

ProwessApps

Learn · Practice · Excel

Java Basics

Keywords in Java

Java keywords are reserved words with predefined roles in the language grammar. They define types, declarations, access control, control flow, exception handling, object creation, and other parts of Java syntax.

10 min read Beginner Lesson 9 of 124
Lesson 9 of 124 in the Java roadmap

Quick answer

What are Java keywords?

Java keywords are words reserved by the language grammar. A reserved keyword such as class, if, or public cannot be used as an ordinary identifier, including a variable, method, or class name.

What you will learn

  • What reserved keywords mean in Java.
  • How keywords differ from contextual keywords and literals.
  • How Java keywords are grouped by purpose.
  • Why const and goto remain reserved.
  • Why a keyword cannot be used as an identifier.

Before you begin

Review Java Comments and First Java Program. The examples use a class and the standard main() method.

Key points about Java keywords

  • Reserved keywords have special grammatical meaning.
  • Reserved keywords cannot be ordinary identifiers.
  • Keywords are lowercase and Java is case-sensitive.
  • true, false, and null are literals rather than keywords, but they cannot be identifiers.
  • Some newer language terms are contextual keywords and are restricted only in specific grammatical locations.

Complete Java reserved keyword list

The table includes Java's reserved keyword tokens, including the reserved but unused words const and goto, plus the underscore token _.

abstractassert Java 1.4booleanbreak
bytecasecatchchar
classconstcontinuedefault
dodoubleelseenum Java 5
extendsfinalfinallyfloat
forgotoifimplements
importinstanceofintinterface
longnativenewpackage
privateprotectedpublicreturn
shortstaticstrictfp Java 1.2super
switchsynchronizedthisthrow
throwstransienttryvoid
volatilewhile_ Java 9

Why lists on older websites may differ

Older tutorials often show the traditional set of 50 words. Modern Java specifications distinguish reserved keywords, contextual keywords, and reserved literals. Additionally, the underscore token _ has special reserved status (it is now used in modern Java for unnamed variables and patterns).

ADVERTISEMENT

Java keyword categories

CategoryKeywordsPurpose
Primitive types and return typeboolean byte char short int long float double voidDeclare primitive types or indicate no returned value.
Control flowif else switch case default for while do break continue returnControl decisions, loops, branching, and method return.
Classes and objectsclass interface enum extends implements new this super instanceofDeclare types, create objects, and describe relationships.
Access and modifierspublic protected private static final abstract synchronized volatile transient native strictfpControl accessibility and declaration behavior.
Exception handlingtry catch finally throw throws assertHandle failures, throw exceptions, and express assertions.
Packagespackage importOrganize code and reference types from other packages.
Reserved but unusedconst gotoRemain reserved and cannot be identifiers.

Reserved but unused keywords

const and goto are reserved, but Java does not assign them normal language functionality. Use final when a variable should not be reassigned; Java does not use const for that purpose.

const

Reserved and unavailable as an identifier. Java normally uses final for non-reassignable variables.

goto

Reserved and unavailable as an identifier. Java uses structured control-flow statements instead.

Contextual keywords in modern Java

A contextual keyword has a special meaning only in particular grammar contexts. Outside those contexts, some contextual keywords may still be usable as identifiers. Examples in modern Java include terms used by modules, records, sealed classes, type inference, switch expressions, and pattern matching.

AreaExamples
Modulesmodule open opens exports requires transitive to uses provides with Java 9
Local type inferencevar Java 10
Records and sealed typesrecord sealed permits non-sealed Java 14+
Expressions and patternsyield when Java 14+

Reserved versus contextual

A fully reserved keyword is forbidden as an ordinary identifier everywhere. A contextual keyword is interpreted specially only where the grammar requires it. For example, because var is contextual, this snippet is perfectly legal (though confusing) in modern Java:

Legal in Java 10+
var var = 10; // The first 'var' is the type, the second is the variable name!

true, false, and null are literals

true and false are boolean literals, while null is the null literal. They may look like keywords, but they represent literal values. They still cannot be used as identifiers.

Example using Java keywords

This program uses public, class, static, void, int, if, and else.

KeywordExample.java
public class KeywordExample {
    public static void main(String[] args) {
        int score = 80;

        if (score >= 50) {
            System.out.println("Pass");
        } else {
            System.out.println("Try again");
        }
    }
}

Output

Pass

What happens if a keyword is used as an identifier?

The compiler rejects the declaration because class is already a reserved keyword.

Incorrect
int class = 10;
Correct
int classCount = 10;

Common mistakes with Java keywords

  • Using a reserved keyword as a variable, method, or class name.
  • Assuming uppercase text such as IF has the same meaning as if.
  • Calling true, false, and null keywords instead of literals.
  • Assuming every modern term such as record behaves like a fully reserved keyword in every location.
  • Using const instead of final.
  • Memorizing a list without learning the purpose of each category.

What’s next?

Continue with Java Variables to learn how identifiers, types, values, and declarations work together.

Lesson summary

  • Java reserved keywords have predefined grammatical roles.
  • Reserved keywords cannot be used as ordinary identifiers.
  • const and goto are reserved but unused.
  • Modern Java also defines contextual keywords for specific grammar positions.
  • true, false, and null are literals rather than keywords.
  • Keywords are case-sensitive and should be learned by category and purpose.

Frequently asked questions

What are Java keywords?

Java keywords are reserved words with predefined grammatical roles. They cannot be used as ordinary identifiers such as variable, method, or class names.

Can a Java keyword be used as a variable name?

No. A reserved keyword cannot be used as an ordinary identifier. Choose a descriptive alternative such as classCount instead of class.

Are true, false, and null Java keywords?

No. true and false are boolean literals, and null is the null literal. They are not keywords, but they still cannot be used as identifiers.

What are contextual keywords in Java?

Contextual keywords have special meaning only in particular grammar contexts. Examples include module-related terms, var, record, sealed, permits, yield, and when.

Are const and goto used in Java?

No. const and goto are reserved but unused. They cannot be identifiers. Java commonly uses final where a non-reassignable variable is required.

Are Java keywords case-sensitive?

Yes. Java keywords are lowercase and Java is case-sensitive. For example, if is a keyword, while IF is not the if keyword.

Check your knowledge

ADVERTISEMENT

Free learner features

Save your learning progress

Sign in to mark lessons as completed, save your Java learning progress across devices, and participate in lesson discussions.

Sign in to track progress

Discussion

Ask questions, share suggestions, or discuss this lesson.


No approved comments yet. Start the discussion by asking a helpful question.

Java Tutorial for Beginners

Choose a lesson