When learning Java, one of the most important concepts you’ll encounter is keywords. Keywords are the reserved words in Java that have a predefined meaning and purpose. They are part of the syntax of the language and cannot be used as identifiers (like variable names, class names, or methods).
For example, words like class, public, static, and void are all keywords in Java.
What Are Keywords in Java?
-
Definition: Keywords are special words that are reserved by the Java language.
-
Purpose: They define the structure and rules of the program.
-
Restriction: You cannot use them as names for variables, methods, or classes.
For example:
int class = 10; // ❌ Invalid, because 'class' is a keyword
int number = 10; // ✅ Valid
Characteristics of Java Keywords
-
All keywords are case-sensitive and must be written in lowercase.
-
Example:
Class(with capital C) is not a keyword, butclassis.
-
-
Keywords are reserved and cannot be redefined.
-
They define the syntax and structure of Java programs.
List of Java Keywords
Java has 50 keywords (as of Java 17). Here are the most commonly used ones grouped by category:
1. Access Modifiers
-
public,private,protected,default
2. Class, Method, and Object Related
-
class,interface,extends,implements,new,this,super
3. Control Flow
-
if,else,switch,case,default,while,do,for,break,continue,return
4. Data Types
-
int,float,double,char,boolean,byte,short,long,void
5. Exception Handling
-
try,catch,finally,throw,throws
6. Others
-
static,final,abstract,synchronized,volatile,transient,native,package,import
Example Program Using Keywords
Here’s a simple Java program that demonstrates keywords in action:
public class KeywordExample {
public static void main(String[] args) {
int number = 10; // 'int' is a keyword
if (number > 5) { // 'if' is a keyword
System.out.println("Number is greater than 5");
} else { // 'else' is a keyword
System.out.println("Number is 5 or less");
}
}
}
Output:
Number is greater than 5
Conclusion
Keywords are the building blocks of Java syntax. They help define the structure of programs and control the flow of execution. As a beginner, understanding and memorizing Java keywords is essential because they form the foundation for writing correct and error-free code.
Comments
Post a Comment