Java Keywords
1. What is a Keyword in Java?
A keyword in Java is a reserved word that has a special meaning to the Java compiler.
We cannot use keywords as variable names, class names, or method names.
Example:
• int is a keyword.
• class is a keyword.
• public is a keyword.
If we try to use them as variable names, it will give an error.
Example (Wrong):
int class = 10; // Error because "class" is a keyword
2. Why Keywords are Important?
Keywords help the compiler understand:
• Data types
• Access permissions
• Loop structures
• Conditional statements
• Object creation
• Exception handling
They form the basic structure of a Java program.
3. Commonly Used Java Keywords (With Simple Meaning)
1. Data Type Keywords
• int → Used to store integer values.
• float → Used to store decimal values.
• double → Used to store large decimal values.
1
• char → Used to store a single character.
• boolean → Stores true or false.
Example:
int age = 20;
float price = 10.5f;
boolean isPassed = true;
2. Access Modifier Keywords
• public → Accessible from anywhere.
• private → Accessible only inside the class.
• protected → Accessible within package and subclasses.
Example:
public class Student {
private int roll;
}
3. Class & Object Related Keywords
• class → Used to create a class.
• new → Used to create an object.
• this → Refers to current object.
• super → Refers to parent class object.
Example:
class Car {
}
Car c = new Car();
4. Conditional Keywords
• if → Used for condition checking.
• else → Executes when if condition is false.
2
• switch → Used to select one option from many.
Example:
if (age > 18) {
[Link]("Adult");
}
5. Loop Keywords
• for → Loop with fixed iterations.
• while → Loop until condition is true.
• do → Executes at least once.
• break → Stops the loop.
• continue → Skips one iteration.
Example:
for(int i=0; i<5; i++) {
[Link](i);
}
6. Other Important Keywords
• static → Belongs to class, not object.
• final → Cannot be changed.
• void → No return value.
• return → Returns a value from method.
• try , catch , finally → Used for exception handling.
4. Total Number of Java Keywords
Java has 50 reserved keywords (in modern Java versions).
5. Important Points to Remember
1. Keywords are always written in lowercase.
2. We cannot use them as identifiers.
3
3. They are fixed and predefined by Java.
Conclusion
Java keywords are special reserved words that define the structure and behavior of a Java program.
Without keywords, we cannot write a valid Java program.