Java keywords
In Java, keywords are reserved words that have a predefined meaning in the language. They form the foundation of Java's syntax and cannot be used as identifiers, such as variable names, method names, or class names.
Each keyword has a specific function, and the Java compiler uses these to parse the structure of Java code.
List of Java Keywords
Keyword | Description |
abstract | Defines abstract classes and methods. |
assert | Used for debugging purposes to test assumptions in the code. |
boolean | Defines a boolean variable with true or false values. |
break | Exits a loop or a switch statement. |
byte | Defines a byte variable (8-bit signed integer). |
case | Defines a block of code in switch statements. |
catch | Handles exceptions in a try-catch block. |
char | Defines a character variable. |
class | Declares a class. |
continue | Skips the current iteration of a loop and continues. |
default | Defines the default block in a switch statement. |
do | Executes a block of code at least once in a do-while loop. |
double | Defines a double precision floating-point variable. |
else | Defines the alternative block for an if statement. |
enum | Declares an enumerated type. |
extends | Indicates a subclass inherits from a superclass. |
final | Defines constants or methods that cannot be overridden. |
finally | Ensures code execution after a try-catch block. |
float | Defines a floating-point variable. |
for | Declares a loop that repeats a block of code. |
if | Starts a conditional block. |
implements | Indicates that a class implements an interface. |
import | Imports classes or entire packages. |
instanceof | Tests whether an object is an instance of a specific class. |
int | Defines an integer variable. |
interface | Declares an interface. |
long | Defines a long integer variable. |
native | Indicates that a method is implemented in native code. |
new | Creates a new object instance. |
null | Represents the null reference, no value. |
package | Declares a package. |
private | Declares a private member, accessible only within its class. |
protected | Declares a protected member, accessible within package or subclasses. |
public | Declares a public member, accessible by any other class. |
return | Exits a method and optionally returns a value. |
short | Defines a short integer variable. |
static | Declares a static member that belongs to the class, not instances. |
strictfp | Restricts floating-point calculations to ensure portability. |
super | Refers to the superclass of the current object. |
switch | Declares a multi-branch conditional statement. |
synchronized | Locks a block of code or method to prevent concurrent access. |
this | Refers to the current object instance. |
throw | Throws an exception. |
throws | Declares exceptions that a method can throw. |
transient | Marks a field to be skipped during serialization. |
try | Starts a block to test for exceptions. |
void | Indicates that a method does not return a value. |
volatile | Marks a variable as being used by multiple threads. |
while | Declares a loop that executes while a condition is true. |
Tips and Best Practices
- Avoid using Java keywords as variable names: Trying to use a keyword as a variable or identifier will result in a syntax error.
- Use meaningful names: When naming classes, methods, or variables, ensure they are descriptive and avoid abbreviations that are unclear.
- Keep keywords in mind when working with frameworks: Be mindful of keywords when you generate classes or methods dynamically, especially when using frameworks that auto-generate code.
- Organize code using keywords effectively: Make good use of keywords like
final
,synchronized
, andvolatile
in multithreaded applications for thread safety and performance.