List of Keywords in Java

List of Keywords in Java

3 mins read3.7K Views Comment
Atul
Atul Harsha
Senior Manager Content
Updated on Oct 12, 2023 17:27 IST

In Java, there are a set of reserved words called “keywords” that have specific meanings in the language and cannot be used as names for variables, methods, or classes. Some examples of Java keywords include “abstract”, “boolean”, and “while”. In this article, we will explore the complete list of Java keywords and their usage.

2022_02_j.jpg

Java has a set of reserved words that have a specific meaning in the language and cannot be used as names for variables, methods, or classes. These reserved words are called “keywords.” In this blog we will see the list of keywords used in Java.

Java keywords list – BytesofGigabytes

Things to Note while using Keywords in Java:

  • Keywords must be spelled exactly as they are written (case-sensitive). For example, “while” is a keyword, but “While” or “WHILE” are not.
  • Keywords cannot be used as names for variables, methods, or classes. For example, the following code will give a compile-time error:

 
int while = 5; // Invalid: "while" is a keyword
void for() { // Invalid: "for" is a keyword
}
class true { // Invalid: "true" is a keyword
}
Copy code
  • Keywords are reserved for the Java language and cannot be used as identifiers in any context other than their intended use. For example, the following code will also give a compile-time error:

 
int x = while; // Invalid: "while" is a keyword
void foo() throws Exception { // Invalid: "throws" is a keyword
}
true = false; // Invalid: "true" and "false" are keywords
Copy code

List of Keywords in Java

There are a total of 53 keywords in Java. Here is a list of Keywords used in Java

Keyword Used To
abstract Declare an abstract method or an abstract class
assert Test a boolean condition at runtime
boolean Declare a boolean variable or a method that returns a boolean value
break Exit a loop or switch statement
byte Declare a byte variable or a method that returns a byte value
case Specify a branch of code in a switch statement
catch Handle exceptions thrown by a try block
char Declare a char variable or a method that returns a char value
class Declare a class
const Declare a constant field (no longer used in Java)
continue Skip the rest of the current iteration of a loop
default Specify a branch of code to be executed if no case matches in a switch statement
do Create a do-while loop
double Declare a double variable or a method that returns a double value
else Specify a branch of code to be executed if the condition is false in an if statement
enum Declare an enumeration
extends Specify that a class or an interface is a subclass or a subinterface of another class or interface
final Declare a final variable or a final class or method
finally Specify a block of code that is always executed after a try block, whether an exception is thrown or not
float Declare a float variable or a method that returns a float value
for Create a for loop
goto Transfer control to a labeled statement (no longer used in Java)
if Specify a branch of code to be executed if a condition is true
implements Specify that a class or an interface is implementing another interface
import Import a package or a class
instanceof Test whether an object is an instance of a class or an interface
int Declare an int variable or a method that returns an int value
interface Declare an interface
long Declare a long variable or a method that returns a long value
native Declare a native method (a method implemented in a language other than Java)
new Create an instance of a class or an array
package Declare a package
private Declare a private field or a private method
protected Declare a protected field or a protected method
public Declare a public field or a public method
return Return a value from a method
short Declare a short variable or a method that returns a short value
static Declare a static field or a static method
strictfp Specify that a method or a class should be executed in strict floating-point mode
super Refer to the superclass of a class
switch Create a switch statement
synchronized Specify that a method can be accessed by only one thread at a time
this Refer to the current object
throw Throw an exception
throws Specify that a method throws an exception
transient Declare a transient field (a field that is not serialized)
try Create a try block
void Declare a method that does not return a value
volatile Declare a volatile field (a field that can be modified by multiple threads)
while Create a while loop
true A boolean literal representing true
false A boolean literal representing false
null A null literal representing a null reference

Related Reads: Data Types in Java

About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio