keywords-and-identifiers
Keywords in Java are a set of predefined, reserved words that form the core vocabulary of the language. These words are integral to the structure and syntax of Java, each serving a unique and essential purpose in how programs are written and executed. Java keywords represent specific commands or instructions to the compiler, such as defining classes, controlling program flow, declaring data types, or handling exceptions.
Because each keyword has a fixed meaning within the Java language, they are strictly reserved and cannot be repurposed or used as identifiers. This means you cannot use any keyword as the name for your variables, methods, classes, interfaces, or any other user-defined elements in your code. Attempting to do so will result in a compilation error, as the compiler would not be able to distinguish between your intended use and the keyword’s built-in function.
Because each keyword has a fixed meaning within the Java language, they are strictly reserved and cannot be repurposed or used as identifiers. This means you cannot use any keyword as the name for your variables, methods, classes, interfaces, or any other user-defined elements in your code. Attempting to do so will result in a compilation error, as the compiler would not be able to distinguish between your intended use and the keyword’s built-in function.
Key Point
- Keywords are case-sensitive and must be used exactly as defined (e.g., while is a keyword, but While is not).
- They form the foundation of Java’s syntax and control program structure, flow, and features.
- You cannot use keywords as identifiers (variable, class, or method names).
Complete List of Java Keywords
Java currently has 50+ reserved keywords (the exact number can vary slightly by Java version and context). Here are some of the most important ones, grouped by category:
Java currently has 50+ reserved keywords (the exact number can vary slightly by Java version and context). Here are some of the most important ones, grouped by category:
Keyword | Description |
---|---|
abstract | Declares abstract classes or methods |
assert | Used for debugging |
boolean | Declares a boolean variable |
break | Exits a loop or switch |
byte | Declares a byte variable |
case | Defines a case in a switch statement |
catch | Handles exceptions |
char | Declares a character variable |
class | Declares a class |
continue | Skips to the next iteration of a loop |
default | Default case in switch |
do | Starts a do-while loop |
double | Declares a double variable |
else | Alternative block for if |
enum | Declares an enumeration |
extends | Indicates inheritance |
final | Defines constants or prevents overriding/inheritance |
finally | Executes after try-catch |
float | Declares a float variable |
for | Starts a for loop |
if | Starts a conditional block |
implements | Indicates interface implementation |
import | Imports classes/packages |
instanceof | Checks object type |
int | Declares an integer variable |
interface | Declares an interface |
long | Declares a long variable |
native | Indicates native method |
new | Creates new objects |
package | Declares a package |
private | Private access modifier |
protected | Protected access modifier |
public | Public access modifier |
return | Returns from a method |
short | Declares a short variable |
static | Declares a static member |
strictfp | Ensures floating-point consistency |
super | Refers to superclass |
switch | Starts a switch statement |
synchronized | Ensures thread safety |
this | Refers to current object |
throw | Throws an exception |
throws | Declares exceptions thrown |
transient | Skips field during serialization |
try | Starts a try block |
void | Indicates no return value |
volatile | Marks variable for thread safety |
while | Starts a while loop |
Special reserved literals:
true, false, null (these are not technically keywords, but are reserved for literal values).
true, false, null (these are not technically keywords, but are reserved for literal values).
Contextual Keywords
Java also has contextual (restricted) keywords introduced in newer versions (Java 9+). These words act as keywords only in certain contexts, allowing them to be used as identifiers elsewhere. Examples include:
Java also has contextual (restricted) keywords introduced in newer versions (Java 9+). These words act as keywords only in certain contexts, allowing them to be used as identifiers elsewhere. Examples include:
- exports, module, open, opens, permits, provides, record, requires, sealed, to, transitive, uses, var, with, yield, non-sealed
Note: Some contextual keywords (like record, sealed, var, yield, permits) cannot be used as class names or type identifiers in recent Java versions.
Unused Reserved Keywords
Some words are reserved but not currently used in Java. These include:
Example: Valid Identifiers
Example: Invalid Identifiers
Some words are reserved but not currently used in Java. These include:
- const (use final instead)
- goto (not supported in Java, as it leads to poor code structure)
- strictfp (used for floating-point consistency, but rarely needed)
What Are Identifiers in Java?
Identifiers are names you assign to variables, methods, classes, packages, and interfaces. They help you reference and organize your code.
Identifiers are names you assign to variables, methods, classes, packages, and interfaces. They help you reference and organize your code.
Key Point
- Identifiers are user-defined names for program elements.
- They must start with a letter (A-Z, a-z), underscore (_), or dollar sign ($).
- Subsequent characters can be letters, digits (0-9), underscores, or dollar signs.
- Identifiers are case-sensitive (MyVar and myvar are different).
- No spaces or special characters (like @, #, %) are allowed.
- Cannot be a Java keyword or reserved word.
- There is no length limit, but concise, meaningful names are recommended.
Example: Valid Identifiers
int age;String firstName;double _salary;float $rate;
Example: Invalid Identifiers
int 1stValue; // Cannot start with a digitfloat my-rate; // Hyphens not allowedString class; // 'class' is a keywordint my value; // Spaces not allowed
Best Practices for Identifiers
- Use descriptive names (e.g., employeeCount instead of ec).
- Follow camelCase for variables and methods (totalAmount, calculateSum).
- Use PascalCase for class names (EmployeeDetails).
- Avoid starting identifiers with $ or _ unless required.
- Do not use Java keywords as identifiers.
Special Note: const and goto in Java
Example: Keyword Vs. Identifiers
- const and goto are reserved but not used in Java.
- Use final for constants instead of const.
- Java does not support goto; use structured control statements like if, for, and while instead.
Example: Keyword Vs. Identifiers
public class Addition { // 'class' is a keyword, 'Addition' is an identifier public static void main(String[] args) { // 'public', 'static', 'void' are keywords; 'main', 'args' are identifiers int a = 10; // 'int' is a keyword, 'a' is an identifier int b = 20; // 'int' is a keyword, 'b' is an identifier int sum = a + b; // 'sum' is an identifier System.out.println(sum); // 'System', 'out', 'println' are identifiers (from Java library) }}
Understanding the difference between keywords and identifiers is fundamental in Java.
- Keywords are reserved words that define the structure and rules of the language-they cannot be used for naming.
- Identifiers are names you create for your program’s elements-follow the rules to ensure your code is readable and error-free.