Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Keywords



In Java, keywords are reserved words that have a specific meaning to the compiler. These words cannot be used as identifiers (like variable names, class names, or method names). They define the structure and flow of a Java program.

There are 50+ Java keywords (as of Java 17), each playing a unique role. Let’s walk through each keyword with explanations and examples where applicable.

Java Keywords Table

Keyword Description
abstractDefines an abstract class or method without implementation.
assertUsed for debugging; tests assumptions in code.
booleanDefines a variable with true or false value.
breakExits the current loop or switch block.
byteDefines a variable of 8-bit signed integer.
caseDefines individual conditions in a switch statement.
catchHandles exceptions thrown in the try block.
charDeclares a single 16-bit Unicode character.
classDeclares a class in Java.
constReserved but not used in Java.
continueSkips current iteration in loop and continues with the next.
defaultSpecifies default block in switch or method in interface.
doStarts a do-while loop.
doubleDeclares a 64-bit floating-point number.
elseSpecifies the block to run if condition in if is false.
enumDefines a fixed set of constants.
extendsIndicates a subclass or interface extension.
finalUsed to declare constants, prevent method override or class inheritance.
finallyBlock that always executes after try-catch.
floatDeclares a 32-bit floating-point number.
forUsed to start a for loop.
gotoReserved but not used.
ifUsed to execute block based on condition.
implementsUsed when a class implements an interface.
importIncludes other classes or packages.
instanceofTests whether an object is an instance of a specific class or interface.
intDeclares a 32-bit signed integer.
interfaceDeclares a contract with method signatures only.
longDeclares a 64-bit signed integer.
nativeDeclares a method implemented in native (non-Java) code.
newCreates new objects.
nullRepresents no value or reference.
packageSpecifies the package for the class.
privateRestricts access to within the same class.
protectedAccess limited to subclass or same package.
publicAccessible from anywhere.
returnExits from a method and optionally returns a value.
shortDeclares a 16-bit signed integer.
staticUsed for class-level variables and methods.
strictfpEnsures floating point calculations are consistent across platforms.
superRefers to parent class constructor or methods.
switchExecutes block based on matching case.
synchronizedControls thread access to blocks/methods.
thisRefers to current class instance.
throwThrows an exception explicitly.
throwsDeclares exceptions a method might throw.
transientPrevents serialization of a variable.
tryDefines a block to test for exceptions.
voidSpecifies no return type for a method.
volatileMarks variable as changed by different threads.
whileStarts a while loop based on condition.

Example: Using Some Common Java Keywords

public class Demo {
  public static void main(String[] args) {
    int count = 5;

    for (int i = 0; i < count; i++) {
      if (i == 3) {
        continue; // skip 3
      }
      System.out.println("i = " + i);
    }
  }
}
i = 0
i = 1
i = 2
i = 4

Another Example: Try-Catch with Exception Handling

public class ExceptionDemo {
  public static void main(String[] args) {
    try {
      int result = 10 / 0;
    } catch (ArithmeticException e) {
      System.out.println("Cannot divide by zero!");
    } finally {
      System.out.println("Finally block always runs.");
    }
  }
}
Cannot divide by zero!
Finally block always runs.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M