Java Keywords

Keywords in Java

Java keywords are reserved words that have special meanings in the language. They cannot be used as identifiers (such as variable names, class names, or method names). Below is a comprehensive overview of the most important Java keywords, organized into categories, with explanations and examples.In the next section of this course you will see of this keywords are used in java programming.

1. Type keyword

Type keywords are used to declare the type of a variable, define method return types, and more. They form the basis for data handling in Java.

Keyword Description Example
boolean Represents a Boolean value (true or false). boolean isJavaFun = true;
char Represents a single character char initial = ‘D’;
int Represents an integer, whole number int salary = 50000;
float It only gives output with 7-digit numbers with a decimal point or less than 7 if the number of digits is 7 it will round the last number otherwise not float percentage = 19.999999f;
double It only gives output with 15-digit numbers with a decimal point or less than 15 double price =19.99999999;
void Specifies that a method does not return any value public void showMessage() { … }
String Represents a sequence of characters String greeting = “Hello, World!”;

2. Control Flow Keywords

Control flow keywords determine the order in which the statements are executed. They are fundamental to writing logic in your Java programs.

Keyword Description Example
if Begins a conditional statement. if (age > 18) { … }
else Specifies the block to execute if the if condition is false. else { … }
switch Chooses a block of code to execute from multiple options. switch (day) { case 1: … }
case A branch in a switch statement. case 1: System.out.println(“Monday”);
default A fallback block in a switch statement. default: System.out.println(“Invalid”);
for Initiate a loop that runs a specific number of times for (int i = 0; i < 10; i++) { ... }
while Begins a loop that continues as long as a condition is true. while (count < 5) { ... }
do Starts a loop that executes at least once, then repeats based on a condition. do { … } while (count < 5);
break Exits the current loop or switch statement immediately. break;
continue Skips the current iteration of a loop and proceeds to the next one. continue;
return Exits from the current method and optionally returns a value. return result;

3. Modifier Keywords

Modifier keywords alter the behavior of classes, methods, and variables in Java, influencing aspects like access control and inheritance.
abstract
Used to define abstract classes and methods, which cannot be instantiated and must be implemented by subclasses.

abstract class Animal {
    abstract void sound();
}
class Dog extends Animal {
    void sound() {
        System.out.println(“Bark”);
    }
}

● static
Indicates that a member belongs to the class, rather than instances of the class. Static members can be accessed without creating an instance of the class.

class Calculator {
    static int add(int a, int b) {
        return a + b;
    }
}
int sum = Calculator.add(5, 10);

4. Access Modifier Keywords

These keywords control the visibility and accessibility of classes, methods, and variables.
public:
The public keyword allows a class, method, or variable to be accessed from any other class in any package. This means there are no restrictions on visibility, making it widely accessible throughout the entire application.

private:
The private keyword restricts the visibility of a class, method, or variable to the class in which it is defined. This means that no other class, even within the same package, can access or modify private members. It is used to encapsulate and protect the internal state of the class.

protected:
The protected keyword allows a class, method, or variable to be accessed by classes within the same package and by subclasses, even if they are in different packages. This provides a balance between accessibility and protection, often used in inheritance to allow subclasses to use or modify superclass members.

default (Package-Private):
When no access modifier is specified, the default access level is applied, also known as package-private. This means that the class, method, or variable is accessible only within its own package. It cannot be accessed by classes outside the package, providing a moderate level of encapsulation.

5. Object-Oriented Programming Keywords

These keywords are essential for implementing Object-Oriented Programming (OOP) principles in Java.

Keyword Description Example
class Declares a class. class Car { … }
interface Declares an interface, a contract that classes can implement. interface Vehicle { … }
extends Indicates that a class inherits from a superclass. class Sedan extends Car { … }
implements Specifies that a class implements an interface. class Sedan implements Vehicle { … }
new Creates a new instance of a class. Car myCar = new Car();
this Refers to the current object instance. this.name = name;
super Refers to the superclass of the current object. super();
instanceof Checks if an object is an instance of a specific class or subclass. if (myCar instanceof Sedan) { … }

6. Exception Handling Keywords

Java provides a robust mechanism to handle runtime errors through the following keywords.

Keyword Description Example
try Starts a block of code that will be tested for exceptions. try { … }
catch Defines a block of code to handle the exception. catch (Exception e) { … }
finally Executes a block of code after try and catch, regardless of whether an exception was thrown. finally { … }
throw Throws an exception explicitly. throw new IOException(“Error!”);
throws Declares an exception that a method might throw. void method() throws IOException { … }

Example in context:

public class ExceptionExample {
    public static void main(String[] args) {
        // The ‘try’ block contains code that may throw an exception
        try {
            // This line attempts to divide 10 by 0, which will cause an ArithmeticException
            int result = 10 / 0;
        }
        // The ‘catch’ block handles the exception if one is thrown in the ‘try’ block
        catch (ArithmeticException e) {
            // If an ArithmeticException occurs, this block will run
            // It prints the error message associated with the exception
            System.out.println(“Error: ” + e.getMessage());
        }
        // The ‘finally’ block runs whether or not an exception is thrown
        finally {
            // This message is always printed, indicating the completion of the code
            System.out.println(“Execution completed.”);
        }
    }
}

7. Miscellaneous Keywords

These keywords are used in various other contexts in Java.
● package
Declares a namespace for a group of related classes.

package com.example.utils;

● import
Imports other Java packages or classes for use in your class.

import java.util.Scanner;

● enum
Defines a set of named constant values. In variable we can define one value eg: day =Monday enum allows us to define set of values

enum Day {
MONDAY, TUESDAY, WEDNESDAY;
}

● null
Represents the absence of a value or reference.

String name = null;

Course Video

YouTube Reference :

Frequently Asked Questions

Still have a question?

Let's talk

You will learn about various Java operators, their types, and how they are used in programming.

Yes, this training is available for free.

The training is ideal for beginners who want to understand the fundamentals of Java operators.

The training covers arithmetic, relational, logical, assignment, bitwise, unary, and ternary operators.

Basic knowledge of Java is helpful but not mandatory for this training.

Yes, practical examples are included to help you understand the concepts better.

You can access the training online through the Iqra Technology Academy website.

Understanding Java operators helps in performing calculations, comparisons, and decision-making effectively in Java programming.

The page doesn’t specify certification, but you gain valuable knowledge and skills.

Yes, the training material is available online and can be accessed anytime.