Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
List of Keywords in Java
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

Introduction to Keywords in Java

Author Yashesvinee V
0 upvote
Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

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.

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

List of Keywords in Java

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()
   { … }
}

assert

It describes a true-false statement to indicate that the user wants the result to be true.
assert expression1 : expression2; 

boolean

A primitive data type that declares a variable to hold two values - true and false
boolean x = true;

break

A control statement used to break out of loops and switch case blocks.
while(condition)
{
   if (condition)
     break;
}

byte

It is a signed 8-bit primitive data type in Java. Range: -128 to 127
byte a =1;

char

A primitive data type that is used to declare a character variable.
char ch = ‘a’;

case

This is used in switch-case statements to execute blocks of code based on specific events/conditions.
switch(exp)
{
   case x: …
               break;
   .
   .
   default: …
}

catch

It is used to catch the exceptions generated by try statements.
try
{ … }
catch (Exception e) 
{ … }

class

To define and declare a class in Java.
class Class_name
{ … }

continue

A control statement that is used to skip the current iteration and continue to the next.
while(condition)
{
   if (condition)
     continue;
}

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: …
}

do

It is used to start a do-while loop.
do { … }
while (condition);

double

A primitive data type that declares variables to store 64-bit floating point numbers.
double x = 23.46287573734;

else

It is used to declare an alternate set of codes for an if statement.
if (condition)
{ … }
else { … }

enum

A keyword used to declare an enumerated data type that represents a group of constants.
enum enum_name
{  … ,
   constant_n; }

extends

It is used to indicate that a class is derived from another class.
class A { … }
class B extends A { … }

final

A non-access modifier which indicates that the contents of its variable, class or method is constant and final.
final variable1 = 20;

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 
{ … }

float

A primitive data type that declares a variable to hold 32-bit floating-point numbers.
float x = 234.34;

for

It is used to start a for loop.
for ( … ; … ; … )
{ … }

if

It starts an if conditional statement.
if (condition)
{ … }

implements

It indicates that the class implements an interface.
interface A
{ 
   public void methodA();
}
class B implements A
{
   @Override 
   void methodA()
   { … }
}

import

It is used to import a package or class.
import java.util.*;

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;

int

A primitive data type that can hold integers in the range -2147483648 to 2147483647
int x = -3825;

interface

It is used to declare a class for only abstract methods.
interface Interface_name
{
  void methodA( );
}

long

A primitive data type capable of holding very large numbers. (64-bit integers)
long x = 4354748364448;

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();

new

It is used to create new objects of a class or an array.
Class_name obj = new Class_name(); 
int[ ] arr = new int[size];   

package

To declare a new package.
package package_name;

private

An access modifier that allows access of its members only within the declared class.
private void method() { … }

protected

An access modifier that allows access of its members within the package and all of its subclasses.
protected void methodA() { … }

public

An access modifier that allows access of its members to the entire Java universe.
public void methodA() { … }

return

It causes the control to return from a called method back to the calling method.
public void methodB() 
{  …
   return;
}
public void methodA()
{  … 
   methodB();
}

short

A data type that holds 16-bit integers.
short x = 21;

static

A non-access modifier which indicates that it can be accessed without the creation of an object.
public static void main() { … }

strictfp

It restricts the precision and truncation of floating-point calculations to ensure portability.
public strictpf class A { … }

super

It is used to refer to the object of a parent class.
super(argument_list);

switch

It is used to create a multiway branch statement.
switch(exp)
{
   case x: …
         break;
     …
   default: …
}

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() { … }

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; 
   }
}

throw

It is used to throw a declared exception and create a custom error.
throw new exception_type(message);

throws 

throws is used to indicate the exception type that may be thrown by a method.
void methodA() throws exception_type
{ … }

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) 
{ … }

void

It is used to declare that a method does not return any value.
public static void main()

volatile

Volatile specifies that a variable is asynchronously modified by concurrently running threads.
static volatile int var_name = 5;

while

It is used to start a while loop.
while(condition)
{ … }

You can practice on online Java Compiler.

Must Read What are Loops in Java.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

FAQs

Q: 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.

Q: 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.

Key Takeaways

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!

Previous article
Loose Coupling in Java
Next article
Introduction to Variables
Live masterclass