Java Keywords
Keywords in Java are predefined, reserved words that have a special meaning to the Java compiler. These keywords cannot be used as identifiers (e.g., variable names, class names, or method names) because they serve specific roles in the language.
Java has a set of keywords that represent various functionalities such as data types, control structures, modifiers, and error handling. These keywords are case-sensitive and must be used exactly as written.
Categories of Java Keywords
Data Types Keywords: These keywords are used to define various data types in Java.
byte
: 8-bit integershort
: 16-bit integerint
: 32-bit integerlong
: 64-bit integerfloat
: 32-bit floating pointdouble
: 64-bit floating pointchar
: Single 16-bit Unicode characterboolean
: True or false valuesvoid
: Used to specify that a method does not return a value
Control Flow Keywords: These keywords control the flow of execution in a Java program.
if
: Executes a block of code if a specified condition is true.else
: Executes a block of code if the condition in theif
statement is false.switch
: Used to execute one block of code among many based on the value of an expression.case
: Defines a block of code to be executed in theswitch
statement.default
: Defines the block of code to be executed if nocase
matches in theswitch
.for
: Used for loops, iterating a block of code a specified number of times.while
: Executes a block of code as long as the condition is true.do
: Executes a block of code once and then repeats while the condition is true.break
: Exits a loop or switch statement prematurely.continue
: Skips the current iteration of a loop and moves to the next iteration.return
: Exits a method and optionally returns a value.try
: Defines a block of code to be tested for exceptions.catch
: Defines a block of code to handle exceptions.finally
: Defines a block of code that is always executed after the try/catch, whether an exception is handled or not.throw
: Used to explicitly throw an exception.throws
: Declares an exception that a method can throw.
Modifiers Keywords: These keywords are used to define the scope and behavior of classes, methods, and variables.
public
: The member is accessible from anywhere in the program.private
: The member is accessible only within the class.protected
: The member is accessible within the class, subclasses, and same package.static
: Indicates that a member belongs to the class rather than to instances of the class.final
: Prevents a class from being subclassed, a method from being overridden, or a variable from being reassigned.abstract
: Used to declare abstract classes and methods that must be implemented by subclasses.synchronized
: Used to synchronize a block of code or method for thread safety.volatile
: Indicates that a variable may be changed unexpectedly by other threads.transient
: Prevents a variable from being serialized during object serialization.native
: Specifies that a method is implemented in native code (usually in C or C++).strictfp
: Used to restrict floating-point calculations to ensure portability.
Class-related Keywords: These keywords define classes and control their behavior.
class
: Used to define a class.extends
: Specifies that a class inherits from a superclass.implements
: Specifies that a class implements an interface.interface
: Used to define an interface.this
: Refers to the current instance of the class.super
: Refers to the superclass of the current object.new
: Creates new objects.instanceof
: Tests if an object is an instance of a specific class or interface.enum
: Defines a set of named constants (an enumeration).
Error Handling Keywords:
try
,catch
,finally
,throw
,throws
: These keywords are used in exception handling, as explained earlier.
Package-related Keywords: These keywords are used for organizing classes and controlling access.
package
: Specifies the package to which the class belongs.import
: Allows the inclusion of other classes and packages in the current file.
Object-related Keywords:
new
: Used to create new objects.this
: Refers to the current instance of the class.super
: Refers to the parent class and is used to access members of the superclass.
Others:
null
: A literal value representing the absence of an object.true
andfalse
: Literal values for the boolean data type.assert
: Used to test assumptions in the code (introduced in Java 1.4).goto
: Reserved but not used.const
: Reserved but not used.
Example Program with Keywords
public class Example {
public static void main(String[] args) {
int number = 10; // 'int' is a keyword
if (number > 0) { // 'if' and 'else' are keywords
System.out.println("Positive number");
} else {
System.out.println("Non-positive number");
}
}
}
Summary of Java Keywords
Keyword | Description |
---|---|
abstract | Declares an abstract class or method |
assert | Used for debugging purposes |
boolean | Declares a boolean data type |
break | Exits a loop or switch |
byte | Declares a byte data type |
case | Defines cases in a switch statement |
catch | Catches exceptions |
char | Declares a character data type |
class | Declares a class |
const | Not used in Java (reserved) |
continue | Skips the current iteration of a loop |
default | Specifies default case in switch statement |
do | Used in do-while loops |
double | Declares a double data type |
else | Specifies alternative path in if-else statement |
enum | Defines an enumeration |
extends | Specifies a superclass |
final | Declares constants, prevents overriding and inheritance |
finally | Executes code after try-catch block |
float | Declares a floating-point data type |
for | Used for loops |
goto | Not used in Java (reserved) |
if | Specifies a conditional statement |
implements | Specifies that a class implements an interface |
import | Imports other classes or packages |
instanceof | Tests if an object is an instance of a class or interface |
int | Declares an integer data type |
interface | Declares an interface |
long | Declares a long integer data type |
native | Specifies that a method is implemented in native code |
new | Creates new objects |
null | Represents a null reference |
package | Specifies a package |
private | Declares private access modifier |
protected | Declares protected access modifier |
public | Declares public access modifier |
return | Exits a method and optionally returns a value |
short | Declares a short integer data type |
static | Declares static fields or methods |
strictfp | Restricts floating-point calculations for portability |
super | Refers to superclass |
switch | Specifies a switch statement |
synchronized | Used for thread synchronization |
this | Refers to the current object |
throw | Throws an exception |
throws | Declares exceptions thrown by a method |
transient | Prevents serialization of fields |
try | Tries a block of code for exceptions |
void | Specifies no return type |
volatile | Declares a volatile field (used in multithreading) |
while | Specifies a while loop |
Conclusion
Java keywords form the foundation of the language. They are reserved words that perform specific functions and help define the structure and behavior of Java programs. Understanding how these keywords work is essential to mastering Java programming.