Home Java Tutorial Java Keywords

Java Keywords

by Anup Maurya
17 minutes read

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

KeywordMeaning
abstractUsed to declare a class or method as abstract
assertUsed for debugging purposes to test assumptions in the code
booleanUsed to declare a boolean variable
breakUsed to exit from a loop or switch statement
byteUsed to declare a byte variable
caseUsed in switch statements to define different cases
catchUsed to handle exceptions in try-catch blocks
charUsed to declare a character variable
classUsed to declare a class
const*Unused keyword
continueUsed to skip an iteration in a loop
defaultUsed in switch statements to define the default case
doUsed to create a do-while loop
doubleUsed to declare a double-precision floating-point variable
elseUsed to define an alternative code block in an if statement
enumUsed to declare an enumeration
extendsUsed to declare inheritance of a class
finalUsed to declare a constant or a class that cannot be extended
finallyUsed in try-catch blocks to execute a code block regardless of exceptions
floatUsed to declare a floating-point variable
forUsed to create a for loop
goto*Unused keyword
ifUsed to create an if statement
implementsUsed to declare implementation of an interface
importUsed to import classes or packages
instanceofUsed to test if an object is an instance of a class
intUsed to declare an integer variable
interfaceUsed to declare an interface
longUsed to declare a long integer variable
nativeUsed to declare a native method
newUsed to create an instance of a class
packageUsed to define a package for a class
privateUsed to declare a private member of a class
protectedUsed to declare a protected member of a class
publicUsed to declare a public member of a class
returnUsed to return a value from a method
shortUsed to declare a short integer variable
staticUsed to declare a static method or a static variable
strictfpUsed to restrict floating-point calculations to IEEE 754 standards
superUsed to refer to the parent class or invoke a parent class constructor
switchUsed to create a switch statement
synchronizedUsed to create a synchronized block of code
thisUsed to refer to the current object
throwUsed to throw an exception from a method
throwsUsed to declare exceptions thrown by a method
transientUsed to declare a transient variable
tryUsed to create a try-catch block
voidUsed to declare a method that does not return a value
volatileUsed to declare a variable whose value may be modified by different threads
whileUsed to create a while loop
trueA Boolean value that represents true
falseA Boolean value that represents false
nullA 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.

related posts

Leave a Comment

Enable Notifications OK No thanks