EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Java

Keyword

In Java, keywords or reserved words are predefined terms that are used by the language for specific internal processes or actions. As such, these keywords cannot be used as variable names or identifiers; doing so will result in a compile-time error.

Example of Using Java Keywords as Variable Names

// Java Program to Illustrate the Consequences of Using Keywords as Variable Names

// Driver Class
class Example {
    // Main Function
    public static void main(String[] args) {
        // Note "public" is a reserved word in Java
        String public = "Hello, Java!";
        System.out.println(public);
    }
}

Output:

./Example.java:6: error: illegal start of expression
        String public = "Hello, Java!";
        ^
1 error

Java Keywords List

Java contains a set of keywords that are typically highlighted in different colors in an IDE or editor to distinguish them from other words. Here’s a table summarizing the keywords and their associated actions:

Keyword
KeywordUsage
abstractSpecifies that a class or method will be implemented later in a subclass.
assertIndicates that a condition is assumed to be true at a specific point in the program.
booleanA data type that can hold true and false values.
breakA control statement used to exit from loops or switch statements.
byteA data type that can hold 8-bit data values.
caseUsed in switch statements to define code blocks.
catchHandles exceptions thrown by try statements.
charA data type that can hold a single 16-bit Unicode character.
classDeclares a new class.
continueSkips the current iteration of a loop and proceeds to the next iteration.
defaultSpecifies the default block of code in a switch statement.
doBegins a do-while loop.
doubleA data type for 64-bit floating-point numbers.
elseSpecifies the alternative branch in an if statement.
enumUsed to declare an enumerated type.
extendsIndicates that a class is derived from another class or interface.
finalIndicates that a variable holds a constant value or that a method cannot be overridden.
finallyA block of code in a try-catch structure that will always execute.
floatA data type for 32-bit floating-point numbers.
forUsed to start a for loop.
ifTests a condition and executes code based on the result.
implementsSpecifies that a class implements an interface.
importReferences other classes or packages.
instanceofChecks whether an object is an instance of a specific class or implements an interface.
intA data type that can hold a 32-bit signed integer.
interfaceDeclares an interface.
longA data type that can hold a 64-bit signed integer.
nativeSpecifies that a method is implemented in platform-specific code.
newCreates new objects.
nullIndicates that a reference does not point to any object.
packageDeclares a Java package.
privateAn access specifier that restricts access to the class where it is declared.
protectedAn access specifier that allows access to subclasses and classes in the same package.
publicAn access specifier that makes a class, method, or variable accessible throughout the application.
returnSends control and possibly a return value back from a called method.
shortA data type that can hold a 16-bit signed integer.
staticIndicates that a method or variable belongs to the class rather than an instance.
strictfpEnsures floating-point calculations follow strict rules for precision.
superRefers to the superclass of the current object.
switchA statement that executes code based on a specified value.
synchronizedIndicates that a method or block is synchronized for thread safety.
thisRefers to the current object within a method or constructor.
throwUsed to explicitly throw an exception.
throwsSpecifies which exceptions a method can throw.
transientIndicates that a variable is not part of an object’s persistent state.
tryStarts a block of code that will be tested for exceptions.
voidSpecifies that a method does not return a value.
volatileIndicates that a variable may be changed unexpectedly, used in multithreading.
whileStarts a while loop.
sealedDeclares a class that restricts which classes can extend it.
permitsUsed within a sealed class declaration to specify permitted subclasses.

Important Notes on Java Keywords

The keywords const and goto are reserved for potential future use but are not currently utilized in Java.

  • const: Reserved for future use.
  • goto: Reserved for future use.
End of lesson.