Java keywords are set of predefined words provided by java vendor which is also called reserved words. These keywords can’t be used as object names,variable names, method names or class names.
Now, let us go through a java program using java keywords.
Example:
//Java program using keywords
class Employee{
public static void main(String args[]){
//using final and int keyword
int grade=2;
//using if and else keywords
if(grade>2){
System.out.println(“Salary of the employee is less than $50k”);
}
else{
System.out.println(“Salary of the employee is more than $50k”);
}
}
}
Java Keywords List
Till Java 21,there are 53 keywords defined in java.
In the list of keywords, 2 keywords are unimplemented or unused keyword which are const & goto.
In Java true,false and null are reserved literals. These 3 literals can’t be used as identifiers in java program.
In Java, keywords are case sensitive and writing Java keywords in upper case (like VOID instead of void) will throw an error.
Keyword
Description
abstract
Abstract keyword is used for classes and methods. Abstract class can’t be instantiated i.e. one can’t create object of abstract class. An abstract method have no body which can only be used inside abstract class. This Abstract keyword specifies that a class or method will be implemented later, in a subclass
assert
An assert keyword in java is used for debugging purpose. This assert keyword is used to create an assertion, which is a statement that you believe to be true at a particular point in your code.
boolean
boolean keyword is a datatype that can store only true or false values.
break
break keyword is used inside loop control structure and switch statements. break keyword is used to terminate the execution of a loop (such as for loop, while loop,do-while loop) or switch statement prematurely.
byte
byte keyword is a data type that can hold 8-bit data values and can store whole numbers from -128 to 127.
case
case keyword is used in switch statement to mark the blocks of code.
catch
catch keyword is used to catch the exceptions generated by try statements.
char
It is a data type that is used to store a single character
class
class keyword declares a new class.
const
const keyword is reserved but not used
continue
continue keyword is used to continue the next iteration of loop.
default
default keyword specifies the default block of code in a switch statement
do
do keyword is used together with while to create a do-while loop.
double
A data type that can hold 64-bit floating point numbers.
else
else keyword is used to indicate alternative branch in an if statement
enum
enum keyword is used to declare an enumerated type.
An enum is a special “class” that represents a group of constants(unchangeable variables like final variables)
enum keyword is used to create an enum instead of class or interface and separate the constants with a comma.
extends
extends keyword indicates that class is inherited from another class.
final
It is a non-access modifier used for classes,variables and methods. variables declared with final keyword can’t be changed ie. final. Method declared with final keyword can’t be overriden. Class declared with final keyword can’t be instantiated.
finally
finally indicates a block of code in a try-catch structure that will always execute, no matter if there is an exception or not.
float
float is a data type that can hold a 32-bit floating point number.
for
for keyword is used to create a for loop.
goto
goto is a reserved keyword which is not in used.
if
if keyword checks weather an expression or branches are true/false accordingly.
implements
implements keyword specifies that a class implements an interface
import
import keyword is used to import a package, class or interface.
instanceof
instanceof is a keyword which is used to check weather an object is an instance of a specific class or interface.
int
int is a keyword that can hold a 32-bit signed integer
interface
interface keyword is used to declare an interface which contains only abstract methods.
long
A data type that holds a 64-bit integer
native
native keyword specifies that a method is implemented with native platform specific code.
new
new keyword is used to create new objects.
package
package keyword is used to declare a package
private
private keyword is an access specifier which indicates that a method or variable can be accessed only in the class where it’s declared in.
protected
protected keyword is an access specifier which indicate that a method or variable can be accessed only in the class where it is declared in or in a subclass of the class where it’s declared in or other classes in the same package.
public
public keyword is an access specifier used for classes,interfaces,methods,and variables indicating that the item is accessible throughout the application.
return
return keyword finished the execution of a method and it can return a value from a method.
requires
requires keyword specifies required libraries inside a module . It is new keyword in java 9
short
short is a keyword that can store a 16-bit integer.
static
static keyword indicates that a method or variable is a class level variable or method. static methods/attributes can be accessed without creating an object of a class.
strictfp
strictfp keyword is used to restrict the precision and rounding of floating-point calculations.
super
super keyword refers to the superclass objects.
switch
switch keyword is used to select one of many code blocks to be executed,based on the value of an expression.
synchronized
synchronized keyword specifies that a method can be accessed by one thread at a time
this
this keyword refers to the current object in a method or constructor
throw
throw keyword in java is used to throw an exception from a method or any block of code explicitly.
throws
throws keyword in java is used in a method signature to declare the types of checked exceptions that the method might throw during its execution.
transient
transient keyword in java is used to indicate that particular field of a java class should not be serialized.
try
try keyword in java is used to define a block of code that will tested for exceptions while it is being executed
var
var keyword in java is used for to declare a variable.This keyword is new in Java 10.
void
void keyword specifies that a method shouldn’t have a return value
volatile
volatile keyword in java is a modifier that ensures that an attribute’s value is always the same when read from all threads.
while
while keyword is used to declare a while loop in java.