Java Keywords
Java Keywords
Java keywords are reserved words that have a specific meaning in the Java programming language. These keywords are predefined and cannot be used as identifiers, such as variable names, method names, or class names. Understanding these keywords is essential for writing correct and efficient Java code. This tutorial will cover the Java keywords, their categories, and examples of how they are used in programs.
1. What are Java Keywords?
Java keywords are special words that the Java compiler recognizes and interprets as specific instructions. Since these keywords have a predefined meaning, you cannot use them for anything other than their intended purpose. For example, you cannot name a variable class
or static
because these are reserved keywords in Java.
2. List of Java Keywords
Java has 50 reserved keywords, which are categorized based on their functionality. Below is a detailed table listing the most commonly used keywords, their descriptions, and examples of how they are used in Java code.
2.1 Data Type Keywords
These keywords define the data types of variables and methods.
Keyword | Description | Example |
| Defines an 8-bit integer. |
|
| Defines a 16-bit integer. |
|
| Defines a 32-bit integer. |
|
| Defines a 64-bit integer. |
|
| Defines a 32-bit floating-point number. |
|
| Defines a 64-bit floating-point number. |
|
| Defines a single 16-bit Unicode character. |
|
| Defines a variable with two possible values: |
|
| Specifies that a method does not return any value. |
|
2.2 Control Flow Keywords
These keywords control the flow of the program by defining loops, conditional statements, and decision-making structures.
Keyword | Description | Example |
| Executes a block of code if a condition is true. |
|
| Executes a block of code if the condition in the |
|
| Allows multiple possible execution paths based on a variable's value. |
|
| Defines a block of code in a |
|
| Defines a block of code in a |
|
| Repeatedly executes a block of code while a condition is true. |
|
| Executes a block of code once, then repeats the loop while a condition is true. |
|
| Repeatedly executes a block of code a specified number of times. |
|
| Exits a loop or |
|
| Skips the current iteration of a loop and proceeds to the next iteration. |
|
| Exits from a method and optionally returns a value. |
|
2.3 Access Modifier Keywords
These keywords control the visibility and accessibility of classes, methods, and variables.
Keyword | Description | Example |
| The code is accessible from any other class. |
|
| The code is accessible within the same package and subclasses. |
|
| The code is accessible only within the same class. |
|
2.4 Class and Object Keywords
These keywords are used to define classes, objects, and methods.
Keyword | Description | Example |
| Defines a class. |
|
| Defines an interface, which is a contract that classes can implement. |
|
| Indicates that a class is inheriting from a superclass. |
|
| Indicates that a class is implementing an interface. |
|
| Creates new objects. |
|
| Refers to the current instance of a class. |
|
| Refers to the parent class and is used to call the parent class's methods or constructors. |
|
| Defines a class or method that cannot be instantiated and must be subclassed. |
|
| Indicates that a method or variable belongs to the class rather than any instance of the class. |
|
| Prevents a class from being subclassed, a method from being overridden, or a variable from being modified. |
|
2.5 Exception Handling Keywords
These keywords are used to handle exceptions (errors) in Java.
Keyword | Description | Example |
| Defines a block of code to test for errors. |
|
| Defines a block of code to handle errors if they occur. |
|
| Defines a block of code that will execute regardless of whether an error occurs. |
|
| Throws an exception manually. |
|
| Indicates that a method may throw an exception. |
|
2.6 Other Important Keywords
These keywords have specific uses in Java programming.
Keyword | Description | Example |
| Defines a set of named constants. |
|
| Tests whether an object is an instance of a specific class or subclass. |
|
| Ensures that a method or block of code is executed by only one thread at a time. |
|
| Indicates that a variable's value may be changed by different threads. |
|
| Prevents a variable from being serialized. |
|
| Indicates that a method is implemented in a language other than Java (e.g., C or C++). |
|
| Used for debugging purposes to test assumptions in the code. |
|
| Ensures consistent floating-point calculations across different platforms. |
|
| Defines a package (a namespace that organizes classes). |
|
| Includes classes or entire packages into your program. |
|
3. Reserved Keywords (Not Used)
Java also reserves certain keywords for potential future use. These keywords are currently not used in the language but cannot be used as identifiers.
Keyword | Description |
| Reserved for future use; cannot be used as a variable name. |
| Reserved for future use; cannot be used as a variable name. |
Conclusion
Java keywords are the backbone of the language's syntax and serve specific roles in your programs. Understanding these keywords is crucial for writing correct and efficient Java code. Since these words have predefined meanings, they cannot be used for naming variables, classes, or methods.
For more Java tutorials and resources, visit [codeswithpankaj.com](http://codeswithpankaj.com
).
Last updated