In the world of programming, keywords are fundamental building blocks that have special meaning within a language. Java, like other programming languages, reserves certain words to perform specific tasks and control the flow of the program. Understanding these keywords is crucial for writing correct and efficient Java code.
Java has a set of reserved keywords that cannot be used as identifiers (such as variable names, method names, or class names) because they are already defined by the language itself. These keywords have specific meanings and functions within the Java syntax. Knowing them will help you avoid common errors and write cleaner code.
In this tutorial, we'll explore all the reserved keywords in Java along with their meanings and usage examples. This knowledge is essential for any Java developer to ensure they use the language correctly and effectively.
Java has 53 reserved keywords as of Java SE 17. Here's a comprehensive list categorized by their primary functions:
| Keyword | Description |
|---|---|
public | Allows access from any other class in the same or different package. |
protected | Allows access within the same package and subclasses (in different packages). |
private | Allows access only within the same class. |
| Keyword | Description |
|---|---|
static | Belongs to the class rather than any object of the class. |
final | Cannot be changed or overridden. Used for constants and final classes. |
abstract | Must be implemented by subclasses. |
| Keyword | Description |
|---|---|
byte | 8-bit signed two's complement integer. |
short | 16-bit signed two's complement integer. |
int | 32-bit signed two's complement integer. |
long | 64-bit signed two's complement integer. |
float | Single-precision 32-bit IEEE 754 floating point. |
double | Double-precision 64-bit IEEE 754 floating point. |
char | 16-bit Unicode character. |
boolean | Represents true or false values. |
| Keyword | Description |
|---|---|
if | Conditional statement to execute a block of code if a condition is true. |
else | Alternative block of code if the if condition is false. |
switch | Selects one of many code blocks to be executed based on a value. |
while | Loops through a block of code as long as a specified condition is true. |
do-while | Executes a block of code at least once, then repeats the loop if a specified condition is true. |
for | Loops through a block of code a number of times. |
| Keyword | Description |
|---|---|
break | Exits a switch or loop statement. |
continue | Skips the current iteration in a loop and continues with the next one. |
return | Exits a method and returns a value to the caller. |
| Keyword | Description |
|---|---|
class | Defines a class. |
interface | Declares an interface. |
extends | Inherits from another class or implements an interface. |
implements | Implements one or more interfaces. |
new | Creates a new instance of a class. |
| Keyword | Description |
|---|---|
try | Marks a block of code to be tested for errors while it is being executed. |
catch | Catches the exception that was thrown by the try block. |
finally | Executes a block of code after try-catch blocks, regardless of whether an exception was thrown or not. |
throw | Throws an exception explicitly. |
throws | Declares an exception that might be thrown by a method. |
| Keyword | Description |
|---|---|
synchronized | Ensures that only one thread can execute a block of code at a time. |
| Keyword | Description |
|---|---|
this | Refers to the current instance of the class. |
super | Refers to the parent class (superclass). |
void | Indicates that a method does not return any value. |
null | Represents a null reference, or a reference that does not point to any object. |
true | Boolean literal representing true. |
false | Boolean literal representing false. |
instanceof | Checks whether an object is an instance of a specified class or interface. |
Let's explore some usage examples for these keywords:
1public class Example {2public int publicVar;3protected int protectedVar;4private int privateVar;56public void display() {7System.out.println("Public: " + publicVar);8System.out.println("Protected: " + protectedVar);9System.out.println("Private: " + privateVar);10};11}
Public: 0 Protected: 0 Private: 0
1public class ControlFlow {2public static void main(String[] args) {3int number = 10;45if (number > 5) {6System.out.println("Number is greater than 5");7} else {8System.out.println("Number is less than or equal to 5");9}1011switch (number) {12case 10:13System.out.println("Number is 10");14break;15default:16System.out.println("Number is not 10");17}18}19}
Number is greater than 5 Number is 10
1public class JumpStatements {2public static void main(String[] args) {3for (int i = 0; i < 5; i++) {4if (i == 2) {5continue;6}7if (i == 4) {8break;9}10System.out.println(i);11}12}13}
0 1 3
1class Animal {2String name;34public Animal(String name) {5this.name = name;6}78public void display() {9System.out.println("Animal: " + name);10}11}1213public class Main {14public static void main(String[] args) {15Animal myDog = new Animal("Buddy");16myDog.display();17}18}
1public class SynchronizedExample {2private int count = 0;34public synchronized void increment() {5count++;6}78public synchronized int getCount() {9return count;10}1112public static void main(String[] args) throws InterruptedException {13SynchronizedExample example = new SynchronizedExample();14Thread t1 = new Thread(() -> {15for (int i = 0; i < 1000; i++) {16example.increment();17}18});1920Thread t2 = new Thread(() -> {21for (int i = 0; i < 1000; i++) {22example.increment();23}24});2526t1.start();27t2.start();2829t1.join();30t2.join();3132System.out.println("Final count: " + example.getCount());33}34}
| Keyword | Description |
|---|---|
public, protected, private | Access modifiers for classes, methods, and variables. |
static, final, abstract | Non-access modifiers for classes, methods, and variables. |
byte, short, int, long, float, double, char, boolean | Primitive data types. |
if, else, switch, while, do-while, for | Control flow statements. |
break, continue, return | Jump statements. |
class, interface, extends, implements, new | Classes and objects. |
try, catch, finally, throw, throws | Error handling. |
synchronized | Synchronization. |
this, super, void, null, true, false, instanceof | Miscellaneous keywords. |
Now that you have a comprehensive understanding of Java keywords, the next step is to explore Java String Methods. Strings are essential in any programming language, and learning how to manipulate them effectively will greatly enhance your Java skills.
In the next tutorial, we'll dive into various methods available for strings, such as substring, concat, toUpperCase, and many more. You'll learn how to perform common string operations and solve real-world problems using these methods.
Stay tuned!