Table of Contents
Java Keywords are a set of reserved words in the Java programming language that have a special meaning and cannot be used for any other purpose. These keywords are used to define syntax rules and structures in Java, and they cannot be used as variable names, class names, or any other identifier in Java programs. In this tutorial, we will discuss Java keywords, their meaning, and their usage.
Java Keywords List
Keyword | Meaning |
---|---|
abstract | Used to declare a class or method as abstract |
assert | Used for debugging purposes to test assumptions in the code |
boolean | Used to declare a boolean variable |
break | Used to exit from a loop or switch statement |
byte | Used to declare a byte variable |
case | Used in switch statements to define different cases |
catch | Used to handle exceptions in try-catch blocks |
char | Used to declare a character variable |
class | Used to declare a class |
const* | Unused keyword |
continue | Used to skip an iteration in a loop |
default | Used in switch statements to define the default case |
do | Used to create a do-while loop |
double | Used to declare a double-precision floating-point variable |
else | Used to define an alternative code block in an if statement |
enum | Used to declare an enumeration |
extends | Used to declare inheritance of a class |
final | Used to declare a constant or a class that cannot be extended |
finally | Used in try-catch blocks to execute a code block regardless of exceptions |
float | Used to declare a floating-point variable |
for | Used to create a for loop |
goto* | Unused keyword |
if | Used to create an if statement |
implements | Used to declare implementation of an interface |
import | Used to import classes or packages |
instanceof | Used to test if an object is an instance of a class |
int | Used to declare an integer variable |
interface | Used to declare an interface |
long | Used to declare a long integer variable |
native | Used to declare a native method |
new | Used to create an instance of a class |
package | Used to define a package for a class |
private | Used to declare a private member of a class |
protected | Used to declare a protected member of a class |
public | Used to declare a public member of a class |
return | Used to return a value from a method |
short | Used to declare a short integer variable |
static | Used to declare a static method or a static variable |
strictfp | Used to restrict floating-point calculations to IEEE 754 standards |
super | Used to refer to the parent class or invoke a parent class constructor |
switch | Used to create a switch statement |
synchronized | Used to create a synchronized block of code |
this | Used to refer to the current object |
throw | Used to throw an exception from a method |
throws | Used to declare exceptions thrown by a method |
transient | Used to declare a transient variable |
try | Used to create a try-catch block |
void | Used to declare a method that does not return a value |
volatile | Used to declare a variable whose value may be modified by different threads |
while | Used to create a while loop |
true | A Boolean value that represents true |
false | A Boolean value that represents false |
null | A special literal that represents a null value |
*Note: const and goto are keywords that were reserved for future use but are not currently used in Java.
Usage of Java Keywords
Java keywords have specific meanings and can only be used in certain contexts. Here are some examples of how Java keywords are used in Java programs:
- Declaring a variable:
To declare a boolean variable, we use the keyword “boolean” followed by the variable name:
boolean flag = true;
- Defining a method:
To define a method, we use the keyword “public” followed by the method’s return type, followed by the method name and its arguments:
public int add(int a, int b) {
return a + b;
}
- Creating an object:
To create an object, we use the keyword “new” followed by the class name and the constructor arguments:
MyClass myObject = new MyClass(arg1, arg2);
- Using an if statement:
To create an if statement, we use the keyword “if” followed by a condition in parentheses and the code to be executed if the condition is true:
if (x > y) {
System.out.println("x is greater than y");
}
Conclusion
Java keywords are a fundamental part of the Java programming language. They have predefined meanings and cannot be used for any other purpose. By understanding the usage and meaning of Java keywords, developers can write efficient and effective Java code.