Java Keywords

Java Keywords

Java keywords are reserved words that have a specific meaning in the Java programming language. These keywords are predefined and cannot be used as identifiers, such as variable names, method names, or class names. Understanding these keywords is essential for writing correct and efficient Java code. This tutorial will cover the Java keywords, their categories, and examples of how they are used in programs.


1. What are Java Keywords?

Java keywords are special words that the Java compiler recognizes and interprets as specific instructions. Since these keywords have a predefined meaning, you cannot use them for anything other than their intended purpose. For example, you cannot name a variable class or static because these are reserved keywords in Java.


2. List of Java Keywords

Java has 50 reserved keywords, which are categorized based on their functionality. Below is a detailed table listing the most commonly used keywords, their descriptions, and examples of how they are used in Java code.


2.1 Data Type Keywords

These keywords define the data types of variables and methods.

Keyword

Description

Example

byte

Defines an 8-bit integer.

byte b = 10;

short

Defines a 16-bit integer.

short s = 1000;

int

Defines a 32-bit integer.

int num = 12345;

long

Defines a 64-bit integer.

long l = 123456789L;

float

Defines a 32-bit floating-point number.

float f = 3.14f;

double

Defines a 64-bit floating-point number.

double d = 3.14159;

char

Defines a single 16-bit Unicode character.

char c = 'A';

boolean

Defines a variable with two possible values: true or false.

boolean isJavaFun = true;

void

Specifies that a method does not return any value.

void methodName() { }


2.2 Control Flow Keywords

These keywords control the flow of the program by defining loops, conditional statements, and decision-making structures.

Keyword

Description

Example

if

Executes a block of code if a condition is true.

if (condition) { }

else

Executes a block of code if the condition in the if statement is false.

else { }

switch

Allows multiple possible execution paths based on a variable's value.

switch (value) { }

case

Defines a block of code in a switch statement.

case 1: { }

default

Defines a block of code in a switch statement if no case matches.

default: { }

while

Repeatedly executes a block of code while a condition is true.

while (condition) { }

do

Executes a block of code once, then repeats the loop while a condition is true.

do { } while (condition);

for

Repeatedly executes a block of code a specified number of times.

for (int i = 0; i < n; i++) { }

break

Exits a loop or switch statement prematurely.

break;

continue

Skips the current iteration of a loop and proceeds to the next iteration.

continue;

return

Exits from a method and optionally returns a value.

return value;


2.3 Access Modifier Keywords

These keywords control the visibility and accessibility of classes, methods, and variables.

Keyword

Description

Example

public

The code is accessible from any other class.

public class MyClass { }

protected

The code is accessible within the same package and subclasses.

protected void method() { }

private

The code is accessible only within the same class.

private int number = 10;


2.4 Class and Object Keywords

These keywords are used to define classes, objects, and methods.

Keyword

Description

Example

class

Defines a class.

class MyClass { }

interface

Defines an interface, which is a contract that classes can implement.

interface MyInterface { }

extends

Indicates that a class is inheriting from a superclass.

class MyClass extends SuperClass { }

implements

Indicates that a class is implementing an interface.

class MyClass implements MyInterface { }

new

Creates new objects.

MyClass obj = new MyClass();

this

Refers to the current instance of a class.

this.variable = value;

super

Refers to the parent class and is used to call the parent class's methods or constructors.

super.method();

abstract

Defines a class or method that cannot be instantiated and must be subclassed.

abstract class MyClass { }

static

Indicates that a method or variable belongs to the class rather than any instance of the class.

static int count = 0;

final

Prevents a class from being subclassed, a method from being overridden, or a variable from being modified.

final int MAX = 100;


2.5 Exception Handling Keywords

These keywords are used to handle exceptions (errors) in Java.

Keyword

Description

Example

try

Defines a block of code to test for errors.

try { }

catch

Defines a block of code to handle errors if they occur.

catch (Exception e) { }

finally

Defines a block of code that will execute regardless of whether an error occurs.

finally { }

throw

Throws an exception manually.

throw new Exception();

throws

Indicates that a method may throw an exception.

void method() throws Exception { }


2.6 Other Important Keywords

These keywords have specific uses in Java programming.

Keyword

Description

Example

enum

Defines a set of named constants.

enum Day { MONDAY, TUESDAY, ... }

instanceof

Tests whether an object is an instance of a specific class or subclass.

if (obj instanceof MyClass) { }

synchronized

Ensures that a method or block of code is executed by only one thread at a time.

synchronized void method() { }

volatile

Indicates that a variable's value may be changed by different threads.

volatile int flag;

transient

Prevents a variable from being serialized.

transient int temp;

native

Indicates that a method is implemented in a language other than Java (e.g., C or C++).

native void method();

assert

Used for debugging purposes to test assumptions in the code.

assert condition;

strictfp

Ensures consistent floating-point calculations across different platforms.

strictfp class MyClass { }

package

Defines a package (a namespace that organizes classes).

package com.example;

import

Includes classes or entire packages into your program.

import java.util.Scanner;


3. Reserved Keywords (Not Used)

Java also reserves certain keywords for potential future use. These keywords are currently not used in the language but cannot be used as identifiers.

Keyword

Description

const

Reserved for future use; cannot be used as a variable name.

goto

Reserved for future use; cannot be used as a variable name.


Conclusion

Java keywords are the backbone of the language's syntax and serve specific roles in your programs. Understanding these keywords is crucial for writing correct and efficient Java code. Since these words have predefined meanings, they cannot be used for naming variables, classes, or methods.

For more Java tutorials and resources, visit [codeswithpankaj.com](http://codeswithpankaj.com

).

Last updated