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.
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
constandgotoremain 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, andnullare 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 _.
abstract | assert Java 1.4 | boolean | break |
byte | case | catch | char |
class | const | continue | default |
do | double | else | enum Java 5 |
extends | final | finally | float |
for | goto | if | implements |
import | instanceof | int | interface |
long | native | new | package |
private | protected | public | return |
short | static | strictfp Java 1.2 | super |
switch | synchronized | this | throw |
throws | transient | try | void |
volatile | while | _ 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).
Java keyword categories
| Category | Keywords | Purpose |
|---|---|---|
| Primitive types and return type | boolean byte char short int long float double void | Declare primitive types or indicate no returned value. |
| Control flow | if else switch case default for while do break continue return | Control decisions, loops, branching, and method return. |
| Classes and objects | class interface enum extends implements new this super instanceof | Declare types, create objects, and describe relationships. |
| Access and modifiers | public protected private static final abstract synchronized volatile transient native strictfp | Control accessibility and declaration behavior. |
| Exception handling | try catch finally throw throws assert | Handle failures, throw exceptions, and express assertions. |
| Packages | package import | Organize code and reference types from other packages. |
| Reserved but unused | const goto | Remain 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.
| Area | Examples |
|---|---|
| Modules | module open opens exports requires transitive to uses provides with Java 9 |
| Local type inference | var Java 10 |
| Records and sealed types | record sealed permits non-sealed Java 14+ |
| Expressions and patterns | yield 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:
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.
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.
int class = 10;int classCount = 10;Common mistakes with Java keywords
- Using a reserved keyword as a variable, method, or class name.
- Assuming uppercase text such as
IFhas the same meaning asif. - Calling
true,false, andnullkeywords instead of literals. - Assuming every modern term such as
recordbehaves like a fully reserved keyword in every location. - Using
constinstead offinal. - 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.
constandgotoare reserved but unused.- Modern Java also defines contextual keywords for specific grammar positions.
true,false, andnullare 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
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 progressDiscussion
Ask questions, share suggestions, or discuss this lesson.
No approved comments yet. Start the discussion by asking a helpful question.