Introduction
Java keywords are reserved words that have a predefined meaning in the language. They are used to perform various operations and cannot be used as identifiers (such as variable names, function names, etc.). This cheat sheet provides a quick reference to all the Java keywords along with a brief description of their use.
List of All Java Keywords Cheat Sheet
Here is the list of all the Java keywords with links added to each keyword to learn more with examples.
Keyword | Description |
---|---|
byte |
Used to declare a variable that can hold an 8-bit signed integer. |
short |
Used to declare a variable that can hold a 16-bit signed integer. |
int |
Used to declare a variable that can hold a 32-bit signed integer. |
long |
Used to declare a variable that can hold a 64-bit signed integer. |
float |
Used to declare a variable that can hold a 32-bit floating-point number. |
double |
Used to declare a variable that can hold a 64-bit floating-point number. |
char |
Used to declare a variable that can hold a single 16-bit Unicode character. |
boolean |
Used to declare a variable that can hold a value of true or false. |
var |
Used to declare a variable with inferred type (introduced in Java 10). |
if |
Used to define a conditional statement or block. |
else |
Used in conjunction with if to specify an alternative block of statements. |
for |
Used to define a loop that executes a block of statements a specified number of times. |
while |
Used to define a loop that continues to execute as long as its condition is true. |
do |
Used to define a do-while loop, which executes a block of statements once before checking its condition. |
switch |
Used to define a switch block that selects one of many possible blocks of code to execute. |
case |
Used within a switch block to define a branch. |
break |
Used to exit from a loop or switch block prematurely. |
continue |
Used to skip the current iteration of a loop and proceed to the next iteration. |
default |
Used in a switch block to specify the default case or to define default methods in interfaces (from Java 8). |
yield |
Used in switch expressions to return a value (introduced in Java 13). |
class |
Used to define a class in Java. |
new |
Used to create new objects. |
static |
Used to declare static members that belong to the class rather than any instance. |
interface |
Used to define an interface. |
extends |
Used to indicate that a class is inheriting from a superclass. |
implements |
Used to implement an interface. |
super |
Used to refer to superclass members. |
this |
Used to refer to the current object. |
abstract |
Used to declare abstract classes and methods. |
final |
Used to declare constants and prevent inheritance. |
package |
Used to define a package for the class. |
enum |
Used to define a fixed set of constants. |
private |
Used to declare private fields and methods. |
protected |
Used to declare protected fields and methods. |
public |
Used to declare public classes, fields, methods, and constructors. |
try |
Used to define a block of code to be tested for exceptions. |
catch |
Used to define a block of code to handle exceptions. |
finally |
Used to define a block of code to be executed after a try block, regardless of whether an exception is thrown or not. |
throw |
Used to explicitly throw an exception. |
throws |
Used to declare the exceptions that a method can throw. |
synchronized |
Used to declare synchronized blocks or methods to control the access of multiple threads to shared resources. |
volatile |
Used to indicate that a variable’s value will be modified by different threads. |
module |
Used to define a module (introduced in Java 9). |
exports |
Used to export all public members of a package in a module. |
requires |
Used to specify required libraries inside a module. |
open |
Used to create an open module that grants reflective access to other modules. |
opens |
Used to expose specific packages for reflective access by other modules. |
uses |
Specifies the services consumed by the current module. |
provides |
Specifies services provided by the current module. |
sealed |
Used to define sealed classes and interfaces (introduced in Java 17). |
non-sealed |
Used to define non-sealed classes and interfaces. |
permits |
Used to specify the subclasses that can extend the sealed class directly. |
void |
Indicates that a method does not return any value. |
return |
Used to exit from a method and optionally return a value. |
transient |
Used in serialization to indicate that a variable should not be serialized. |
strictfp |
Ensures strict precision of floating-point calculations across different platforms. |
import |
Used to import other Java packages or classes into the current file. |
instanceof |
Used to test whether an object is an instance of a specific class or implements an interface. |
native |
Specifies that a method is implemented in native code using JNI. |
record |
Used to define a special type of class that acts as a data carrier (introduced in Java 14). |
assert |
Used to make an assertion in debugging. |
const |
Reserved but not used in Java. |
goto |
Reserved but not used in Java. |
_ |
From Java 9, _ (underscore) is a keyword and cannot be used as an identifier. |
Explanation and Examples of Java Keywords
byte
Keyword
The byte
keyword is used to declare primitive byte type variables.
Example:
byte b = 100;
Explanation: Declares a byte variable b
and assigns it the value 100
.
short
Keyword
The short
keyword is used to declare primitive short type variables.
Example:
short s = 1000;
Explanation: Declares a short variable s
and assigns it the value 1000
.
int
Keyword
The int
keyword is used to declare primitive integer type variables.
Example:
int number = 10;
Explanation: Declares an int variable number
and assigns it the value 10
.
long
Keyword
The long
keyword is used to declare primitive long type variables.
Example:
long bigNumber = 100000L;
Explanation: Declares a long variable bigNumber
and assigns it the value 100000
.
float
Keyword
The float
keyword is used to declare primitive float type variables.
Example:
float pi = 3.14f;
Explanation: Declares a float variable pi
and assigns it the value 3.14
.
double
Keyword
The double
keyword is used to declare primitive double type variables.
Example:
double pi = 3.14159;
Explanation: Declares a double variable pi
and assigns it the value 3.14159
.
char
Keyword
The char
keyword is used to declare primitive character type variables.
Example:
char letter = 'A';
Explanation: Declares a char variable letter
and assigns it the value 'A'
.
boolean
Keyword
The boolean
keyword is used to declare primitive boolean type variables.
Example:
boolean isJavaFun = true;
Explanation: Declares a boolean variable isJavaFun
and assigns it the value true
.
var
Keyword
The var
keyword is used to declare a variable of any type (introduced in Java 10).
Example:
var number = 10;
Explanation: The var
keyword infers the type of the variable number
as int
.
if
Keyword
The if
keyword is used to define if condition statements or blocks.
Example:
if (number > 5) {
System.out.println("Number is greater than 5");
}
Explanation: The if
block executes if the condition number > 5
is true.
else
Keyword
The else
keyword is used in if-else blocks.
Example:
if (number < 5) {
System.out.println("Number is less than 5");
} else {
System.out.println("Number is 5 or greater");
}
Explanation: The else
block executes if the if
condition is false.
for
Keyword
The for
keyword is used to define for loops.
Example:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
Explanation: The for
loop iterates from i = 0
to i < 5
.
while
Keyword
The while
keyword is used to define while loops.
Example:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Explanation: The while
loop executes as long as the condition i < 5
is true.
do
Keyword
The do
keyword is used in do-while loops.
Example:
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 5);
Explanation: The do
block executes at least once before checking the condition.
switch
Keyword
The switch
keyword is used to define switch blocks or switch expressions (from Java 12).
Example:
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other day");
}
Explanation: The switch
statement tests the value of day
and executes the corresponding case block.
case
Keyword
The case
keyword is used to define case labels in a switch block.
Example:
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other day");
}
Explanation: The case
statement checks the value of day
and executes the corresponding block of code.
break
Keyword
The break
keyword is used to exit a loop or a block.
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
Explanation: The break
statement exits the for loop when i
equals 5.
continue
Keyword
The continue
keyword is used to stop the execution of the current iteration and start the next iteration in a loop.
Example:
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
continue;
}
System.out.println(i);
}
Explanation: The continue
statement skips the current iteration when i
is even.
default
Keyword
The default
keyword is used in switch blocks to define default case labels and default methods (from Java 8).
Example:
int day = 7;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Other day");
}
Explanation: The default
block executes if none of the case
conditions are met.
yield
Keyword
The yield
keyword is used in switch expressions (from Java 13).
Example:
int day = 2;
String result = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
default -> "Other day";
};
System.out.println(result);
Explanation: The yield
keyword returns a value from a switch expression.
class
Keyword
The class
keyword is used to define a class in Java.
Example:
class MyClass {
int x = 5;
}
Explanation: Declares a class MyClass
with a member variable x
.
new
Keyword
The new
keyword is used to instantiate a class.
Example:
Dog myDog = new Dog();
Explanation: The new
keyword creates a new instance of the Dog
class.
static
Keyword
The static
keyword is used to define static members of a class.
Example:
class MyClass {
static int count = 0;
}
Explanation: The count
variable is static and belongs to the MyClass
class rather than its instances.
interface
Keyword
The interface
keyword is used to define an interface.
Example:
interface Animal {
void makeSound();
}
Explanation: The interface
keyword declares an interface Animal
with an abstract method makeSound()
.
extends
Keyword
The extends
keyword is used when a class is extending or inheriting another class.
Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
Explanation: The Dog
class extends the Animal
class, inheriting its eat()
method.
implements
Keyword
The implements
keyword is used to implement an interface.
Example:
class Dog implements Animal {
public void makeSound() {
System.out.println("Bark");
}
}
Explanation: The Dog
class implements the Animal
interface by providing an implementation of the makeSound()
method.
super
Keyword
The super
keyword is used to access superclass members inside a subclass.
Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void eat() {
super.eat();
System.out.println("Dog is eating...");
}
}
Explanation: The super
keyword calls the eat()
method of the superclass Animal
.
this
Keyword
The this
keyword is used to access other members of the same class.
Example:
class MyClass {
int x;
MyClass(int x) {
this.x = x;
}
}
Explanation: The this
keyword refers to the current instance variable x
.
abstract
Keyword
The abstract
keyword is used to define abstract classes and abstract methods.
Example:
abstract class Animal {
abstract void makeSound();
}
class Dog extends Animal {
void makeSound() {
System.out.println("Bark");
}
}
Explanation: The Animal
class is abstract and has an abstract method makeSound()
. The Dog
class extends Animal
and provides an implementation for the makeSound()
method.
final
Keyword
The final
keyword is used to define final classes and final methods.
Example:
final class MyClass {
final void display() {
System.out.println("Hello");
}
}
Explanation: The final
keyword prevents the MyClass
class from being subclassed and the display()
method from being overridden.
package
Keyword
The package
keyword is used to specify a package for the current file.
Example:
package mypackage;
Explanation: The package
keyword declares that the class belongs to the mypackage
package.
enum
Keyword
The enum
keyword is used to define enum types.
Example:
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
Explanation: The enum
keyword declares an enumeration Day
with constants representing days of the week.
private
Keyword
The private
keyword is used to define private fields, methods, and constructors.
Example:
class MyClass {
private int x = 5;
}
Explanation: The x
variable is private and can only be accessed within the MyClass
class.
protected
Keyword
The protected
keyword is used to define protected fields, methods, and constructors.
Example:
class Animal {
protected void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
Explanation: The eat()
method is protected and can be accessed by the Dog
class, which extends Animal
.
public
Keyword
The public
keyword is used to define public classes, fields, methods, and constructors.
Example:
public class MyClass {
public int x = 5;
}
Explanation: The x
variable is public and can be accessed by any other class.
try
Keyword
The try
keyword is used to define a try block.
Example:
try {
int division = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
Explanation: The try
block contains code that might throw an exception.
catch
Keyword
The catch
keyword is used to define a catch block.
Example:
try {
int division = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}
Explanation: The catch
block catches the ArithmeticException
thrown by the division by zero.
finally
Keyword
The finally
keyword is used to define a finally block.
Example:
try {
int division = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
} finally {
System.out.println("This is the finally block");
}
Explanation: The finally
block executes regardless of whether an exception is thrown or not.
throw
Keyword
The throw
keyword is used to throw an exception.
Example:
void checkAge(int age) {
if (age < 18) {
throw new ArithmeticException("Not eligible to vote");
}
}
Explanation: The throw
keyword throws an ArithmeticException
if the age is less than 18.
throws
Keyword
The throws
keyword is used to specify the exceptions which may be thrown by the current method.
Example:
void myMethod() throws IOException {
throw new IOException("File not found");
}
Explanation: The throws
keyword indicates that the myMethod
can throw an IOException
.
synchronized
Keyword
The synchronized
keyword is used to define synchronized blocks.
Example:
public synchronized void increment() {
count++;
}
Explanation: The synchronized
keyword ensures that the increment
method is accessed by only one thread at a time.
volatile
Keyword
The volatile
keyword is used to define a volatile field whose value is always read from the main memory.
Example:
class MyClass {
volatile int sharedVar;
}
Explanation: The volatile
keyword ensures that the sharedVar
is read from and written to main memory directly.
module
Keyword
The module
keyword is used to define a module (introduced in Java 9).
Example:
module mymodule {
requires java.base;
exports com.example.mypackage;
}
Explanation: The module
keyword declares a module named mymodule
, specifies its dependencies, and exports its packages.
exports
Keyword
The exports
keyword is used to export all public members of a package in a module.
Example:
module mymodule {
exports com.example.mypackage;
}
Explanation: The exports
keyword makes the public members of the com.example.mypackage
package accessible to other modules.
requires
Keyword
The requires
keyword is used to specify required libraries inside a module.
Example:
module mymodule {
requires java.sql;
}
Explanation: The requires
keyword specifies that the mymodule
module depends on the java.sql
module.
open
Keyword
The open
keyword is used to create an open module that grants reflective access to other modules.
Example:
open module mymodule {
requires java.base;
}
Explanation: The open
keyword allows all packages in the mymodule
module to be reflectively accessed by other modules.
opens
Keyword
The opens
keyword is used to expose specific packages for reflective access by other modules.
Example:
module mymodule {
opens com.example.mypackage to some.other.module;
}
Explanation: The opens
keyword allows the some.other.module
module to reflectively access the com.example.mypackage
package.
uses
Keyword
The uses
keyword specifies the services consumed by the current module.
Example:
module mymodule {
uses com.example.MyService;
}
Explanation: The uses
keyword indicates that the mymodule
module depends on the com.example.MyService
service.
provides
Keyword
The provides
keyword specifies services provided by the current module.
Example:
module mymodule {
provides com.example.MyService with com.example.MyServiceImpl;
}
Explanation: The provides
keyword indicates that the mymodule
module provides an implementation of the com.example.MyService
service.
sealed
Keyword
The sealed
keyword is used to define sealed classes and interfaces (introduced in Java 17).
Example:
public sealed class Shape permits Circle, Square {
}
Explanation: The sealed
keyword restricts which other classes can extend or implement the Shape
class.
non-sealed
Keyword
The non-sealed
keyword is used to define non-sealed classes and interfaces.
Example:
public non-sealed class Circle extends Shape {
}
Explanation: The non-sealed
keyword allows the Circle
class to be extended by other classes.
permits
Keyword
The permits
keyword is used to specify the subclasses that can extend the sealed class directly.
Example:
public sealed class Shape permits Circle, Square {
}
Explanation: The permits
keyword specifies that only the Circle
and Square
classes can directly extend the Shape
class.
void
Keyword
The void
keyword indicates that a method returns nothing.
Example:
void display() {
System.out.println("Hello");
}
Explanation: The void
keyword indicates that the display
method does not return a value.
return
Keyword
The return
keyword is used to return a value from a method or a block.
Example:
public int sum(int a, int b) {
return a + b;
}
Explanation: The return
statement exits the sum
method and returns the sum of a
and b
.
transient
Keyword
The transient
keyword is used in serialization to indicate that a variable should not be serialized.
Example:
class MyClass implements Serializable {
transient int x;
}
Explanation: The transient
keyword prevents the x
field from being serialized.
strictfp
Keyword
The strictfp
keyword ensures strict precision of floating-point calculations on different platforms.
Example:
strictfp class MyClass {
double num = 0.0;
}
Explanation: The strictfp
keyword ensures that floating-point calculations conform to IEEE 754 standards.
import
Keyword
The import
keyword is used to import external resources into the current Java file.
Example:
import java.util.ArrayList;
Explanation: The import
statement imports the ArrayList
class from the java.util
package.
instanceof
Keyword
The instanceof
keyword is used to check whether an object is of a specified type.
Example:
Dog myDog = new Dog();
System.out.println(myDog instanceof Animal); // true
Explanation: The instanceof
operator checks if myDog
is an instance of the Animal
class.
native
Keyword
The native
keyword indicates that a method is implemented in native code using JNI.
Example:
public class MyClass {
public native void nativeMethod();
}
Explanation: The native
keyword indicates that the nativeMethod()
is implemented in native code (e.g., C/C++).
record
Keyword
The record
keyword is used to define a special type of classes that act as data carriers (introduced in Java 14).
Example:
public record Point(int x, int y) {
}
Explanation: The record
keyword defines a Point
record class with two fields: x
and y
.
assert
Keyword
The assert
keyword is used in debugging to make an assertion.
Example:
int age = -5;
assert age > 0 : "Age cannot be negative";
Explanation: The assert
statement checks if the age is positive. If not, it throws an AssertionError
with the message “Age cannot be negative”.
const
Keyword
The const
keyword is reserved but not used.
Example:: N/A
goto
Keyword
The goto
keyword is reserved but not used.
Example:: N/A
_
(Underscore)
The _
(underscore) keyword has become a reserved keyword from Java 9 and cannot be used as an identifier.
Example:: N/A
Conclusion
This cheat sheet provides a comprehensive reference to all the Java keywords, helping you understand their usage and enhance your programming skills. Keep this guide handy to ensure you are using these keywords correctly and efficiently in your Java code. Happy coding!