Table of contents
1.
Introduction
2.
List of Java Keywords
3.
Example of Java Programming Keywords
4.
Frequently Asked Questions
4.1.
What are non-access modifiers in Java?
4.2.
What are exceptions in Java?
4.3.
What are keywords also called in Java?
4.4.
Can I use Java keywords as variable names?
4.5.
Differences between 'this' and 'super' keywords in Java:
5.
Conclusion
Last Updated: Oct 26, 2024
Easy

Keywords in Java

Author Yashesvinee V
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Every programming language has a set of reserved words or keywords with special meaning when used in a program code. Keywords define some internal process or pre-defined action when in use. Hence, keywords can not be used as names or identifiers for classes, objects, methods or variables. Doing so would result in a compilation error as the compiler understands the keyword as its pre-defined meaning.

keyword in java

You can also read about the topic of Java Destructor, Duck Number in Java

List of Java Keywords

Java, at present, uses 49 reserved words, excluding the goto and const keywords which are currently not in use. The list of keywords used in Java, along with their meaning and implementation are shown below.

Keyword

Description

Implementation and Use

abstract

A non-access modifier that is used on classes and methods to imply that it would be implemented later in the program.
abstract class A
{  …
   abstract void methodA();
}
class B extends A
{
   @Override 
   void methodA()
   { … }
}
You can also try this code with Online Java Compiler
Run Code

assert

It describes a true-false statement to indicate that the user wants the result to be true.
assert expression1 : expression2; 
You can also try this code with Online Java Compiler
Run Code

boolean

A primitive data type that declares a variable to hold two values - true and false
boolean x = true;
You can also try this code with Online Java Compiler
Run Code

break

A control statement used to break out of loops and switch case blocks.
while(condition)
{
   if (condition)
     break;
}
You can also try this code with Online Java Compiler
Run Code

byte

It is a signed 8-bit primitive data type in Java. Range: -128 to 127
byte a =1;
You can also try this code with Online Java Compiler
Run Code

char

A primitive data type that is used to declare a character variable.
char ch = ‘a’;
You can also try this code with Online Java Compiler
Run Code

case

This is used in switch-case statements to execute blocks of code based on specific events/conditions.
switch(exp)
{
   case x: …
               break;
   .
   .
   default: …
}
You can also try this code with Online Java Compiler
Run Code

catch

It is used to catch the exceptions generated by try statements.
try
{ … }
catch (Exception e) 
{ … }
You can also try this code with Online Java Compiler
Run Code

class

To define and declare a class in Java.
class Class_name
{ … }
You can also try this code with Online Java Compiler
Run Code

continue

A control statement that is used to skip the current iteration and continue to the next.
while(condition)
{
   if (condition)
     continue;
}
You can also try this code with Online Java Compiler
Run Code

default

  • An access modifier that restricts the access of its members outside the declared package.

     
  • It specifies the default block of code in a switch statement.
class A
{
   default void methodA( ) { }
}

switch(exp)
{
   case x: …
         break;
   default: …
}
You can also try this code with Online Java Compiler
Run Code

do

It is used to start a do-while loop.
do { … }
while (condition);
You can also try this code with Online Java Compiler
Run Code

double

A primitive data type that declares variables to store 64-bit floating point numbers.
double x = 23.46287573734;
You can also try this code with Online Java Compiler
Run Code

else

It is used to declare an alternate set of codes for an if statement.
if (condition)
{ … }
else { … }
You can also try this code with Online Java Compiler
Run Code

enum

A keyword used to declare an enumerated data type that represents a group of constants.
enum enum_name
{  … ,
   constant_n; }
You can also try this code with Online Java Compiler
Run Code

extends

It is used to indicate that a class is derived from another class.
class A { … }
class B extends A { … }
You can also try this code with Online Java Compiler
Run Code

final

A non-access modifier which indicates that the contents of its variable, class or method is constant and final.
final variable1 = 20;
You can also try this code with Online Java Compiler
Run Code

finally

It is used with try-catch statements to deal with exceptions. It holds the block of code that is executed irrespective of whether an exception exists or not.
try 
{ … }
catch (Exception e) 
{ … }
finally 
{ … }
You can also try this code with Online Java Compiler
Run Code

float

A primitive data type that declares a variable to hold 32-bit floating-point numbers.
float x = 234.34;
You can also try this code with Online Java Compiler
Run Code

for

It is used to start a for loop.
for ( … ; … ; … )
{ … }
You can also try this code with Online Java Compiler
Run Code

if

It starts an if conditional statement.
if (condition)
{ … }
You can also try this code with Online Java Compiler
Run Code

implements

It indicates that the class implements an interface.
interface A
{ 
   public void methodA();
}
class B implements A
{
   @Override 
   void methodA()
   { … }
}
You can also try this code with Online Java Compiler
Run Code

import

It is used to import a package or class.
import java.util.*;
You can also try this code with Online Java Compiler
Run Code

instanceof

It is used to check whether an object is an instance of a specific class or if it implements an interface.
object_name instanceof class_name;
You can also try this code with Online Java Compiler
Run Code

int

A primitive data type that can hold integers in the range -2147483648 to 2147483647
int x = -3825;
You can also try this code with Online Java Compiler
Run Code

interface

It is used to declare a class for only abstract methods.
interface Interface_name
{
  void methodA( );
}
You can also try this code with Online Java Compiler
Run Code

long

A primitive data type capable of holding very large numbers. (64-bit integers)
long x = 4354748364448;
You can also try this code with Online Java Compiler
Run Code

native

It is used to indicate that the method is implemented in a language other than Java (native platform-specific code )
public native void method();
You can also try this code with Online Java Compiler
Run Code

new

It is used to create new objects of a class or an array.
Class_name obj = new Class_name(); 
You can also try this code with Online Java Compiler
Run Code
int[ ] arr = new int[size];   
You can also try this code with Online Java Compiler
Run Code

package

To declare a new package.
package package_name;
You can also try this code with Online Java Compiler
Run Code

private

An access modifier that allows access of its members only within the declared class.
private void method() { … }
You can also try this code with Online Java Compiler
Run Code

protected

An access modifier that allows access of its members within the package and all of its subclasses.
protected void methodA() { … }
You can also try this code with Online Java Compiler
Run Code

public

An access modifier that allows access of its members to the entire Java universe.
public void methodA() { … }
You can also try this code with Online Java Compiler
Run Code

return

It causes the control to return from a called method back to the calling method.
public void methodB() 
{  …
   return;
}
public void methodA()
{  … 
   methodB();
}
You can also try this code with Online Java Compiler
Run Code

short

A data type that holds 16-bit integers.
short x = 21;
You can also try this code with Online Java Compiler
Run Code

static

A non-access modifier which indicates that it can be accessed without the creation of an object.
public static void main() { … }
You can also try this code with Online Java Compiler
Run Code

strictfp

It restricts the precision and truncation of floating-point calculations to ensure portability.
public strictpf class A { … }
You can also try this code with Online Java Compiler
Run Code

super

It is used to refer to the object of a parent class.
super(argument_list);
You can also try this code with Online Java Compiler
Run Code

switch

It is used to create a multiway branch statement.
switch(exp)
{
   case x: …
         break;
     …
   default: …
}
You can also try this code with Online Java Compiler
Run Code

synchronised

A non-access modifier which tells that the critical sections of a multithreaded code should be executed one thread at a time.
public synchronised void methodA() { … }
You can also try this code with Online Java Compiler
Run Code

this

It is used to reference the current object variable or method. this() can be used to invoke the current class constructor.
class A
{
   public int a;
   public A( int b)
   {
     this.a = b; 
   }
}
You can also try this code with Online Java Compiler
Run Code

throw

It is used to throw a declared exception and create a custom error.
throw new exception_type(message);
You can also try this code with Online Java Compiler
Run Code

throws 

throws is used to indicate the exception type that may be thrown by a method.
void methodA() throws exception_type
{ … }
You can also try this code with Online Java Compiler
Run Code

transient

A non-access modifier which specifies that an attribute is not part of an object's persistent state. 
private transient int variable_name;

try

It is used to start a try-catch block that handles exceptions.
try
{ … }
catch (Exception e) 
{ … }
You can also try this code with Online Java Compiler
Run Code

void

It is used to declare that a method does not return any value.
public static void main()
You can also try this code with Online Java Compiler
Run Code

volatile

Volatile specifies that a variable is asynchronously modified by concurrently running threads.
static volatile int var_name = 5;
You can also try this code with Online Java Compiler
Run Code

while

It is used to start a while loop.
while(condition)
{ … }
You can also try this code with Online Java Compiler
Run Code

You can practice on online Java Compiler.

Must Read What are Loops in Java.

Example of Java Programming Keywords

public class Example {
   public static void main(String[] args) {
       int number = 10; // `int` is a keyword for declaring an integer variable
       final int CONSTANT = 5; // `final` is a keyword to declare a constant
       if (number > CONSTANT) { // `if` is a control-flow keyword
           System.out.println("Number is greater than the constant.");
       } else {
           System.out.println("Number is less than or equal to the constant.");
       }
   }
}
You can also try this code with Online Java Compiler
Run Code


Output

Number is greater than the constant.

Frequently Asked Questions

What are non-access modifiers in Java?

Non-access modifiers in Java can be applied to classes, methods, variables, and constructors to implement additional functionalities and provide information about their behaviour to JVM. The most commonly used modifiers are static, final, volatile, abstract, and synchronised.

What are exceptions in Java?

Exceptions are unexpected events that occur during runtime and disrupt the normal flow of the program’s execution. Throwing an exception involves the creation of an exception object and handing it over to the runtime system. Exceptions can be spotted using the try block, resolved using the catch block and finalised using the finally block.

What are keywords also called in Java?

In Java, keywords are also known as reserved words. They have special meanings and are predefined by the Java language, serving specific purposes within the code structure and control flow.

Can I use Java keywords as variable names?

No, Java keywords cannot be used as variable names because they are reserved by the language for specific functions and commands, ensuring clarity in code syntax and preventing conflicts during compilation.

Differences between 'this' and 'super' keywords in Java:

The this keyword refers to the current instance of a class, allowing access to its members. In contrast, super refers to the superclass instance, enabling access to superclass methods and variables overridden in the subclass.

Conclusion

Keywords are an important part of any program we write. The purpose and behaviour of any variable, class, method or constructor can not be conveyed to the JVM without the help of Keywords. Hence, it is essential to have a deep understanding of them. This blog has covered all the reserved words in Java along with their description and use. We have also discussed implementing them to make your Java program more efficient.

Related Links:

 

You can also consider our paid courses such as DSA in Java to give your career an edge over others!

Live masterclass