Open In App

Why Java is Called a Compiler Interpreter Language?

Improve
Improve
Like Article
Like
Save
Share
Report

Java is both a compiled and interpreted programming language. Because it employs both compilation and interpretation to run code, it is known as a “compiler-interpreter language.” In order for the Java Virtual Machine to understand the Java code, it must first be compiled into an intermediate format called bytecode (JVM). The bytecode is subsequently interpreted by the JVM and transformed into hardware-compatible machine code. Java code can be ported between various operating systems and architectural styles thanks to this two-step approach.

  1. Java code is written in plain text files with the .java extension.
  2. This code is then passed through the Java compiler, which converts it into an intermediate form called bytecode.
  3. Bytecode is a set of instructions that are understood by the Java Virtual Machine (JVM), which is installed on the user’s computer.

The following Java program prints “Hello, World!” to the console:

Java




public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}


Output

Hello, World!
  • When this code is compiled, it is transformed into a bytecode that can be understood by the JVM.
  • The JVM then interprets this bytecode and converts it into machine code that can be executed by the computer’s hardware.

Note: Java also uses the interpretation processes in cases like reflection and dynamic class loading. If a class is loaded dynamically, JVM doesn’t need to compile the class again and again, Instead, it uses an interpretation process to run the class.

Java code that utilizes both compilation and interpretation can be seen below:

This Java code defines a Calculator class that can register and perform different operations, represented by classes that implement the CalculatorOperation interface. The Calculator class has a private map that stores the registered operations, with the operation name as the key and the operation class as the value. The registerOperation method is used to register an operation by adding an entry to this map. The performOperation method takes an operation name and two double values as input, looks up the corresponding operation class in the map, instantiates it, and calls the perform method on the instantiated object to get the result. The Main class shows an example of how to create a Calculator object, register an operation, and perform it. The AddOperation class is an example of an operation class that implements the CalculatorOperation interface and performs addition.

Java




import java.util.*;
  
// Interface for calculator operations
interface CalculatorOperation {
    double perform(double a, double b);
}
  
// Calculator class that can register and perform operations
class Calculator {
    // Map to store registered operations, with operation
    // name as key and operation class as value
    private Map<String,
                Class<? extends CalculatorOperation> >
        operations;
  
    public Calculator() { operations = new HashMap<>(); }
  
    // Method to register a new operation
    public void registerOperation(
        String name,
        Class<? extends CalculatorOperation> operationClass)
    {
        operations.put(name, operationClass);
    }
  
    // Method to perform a registered operation
    public double performOperation(String name, double a,
                                   double b)
    {
        // Look up the operation class in the map
        Class<? extends CalculatorOperation> operationClass
            = operations.get(name);
        if (operationClass == null) {
            throw new IllegalArgumentException(
                "Unknown operation: " + name);
        }
        try {
            // Instantiate the operation class and call its
            // perform method
            CalculatorOperation operation
                = operationClass.getDeclaredConstructor()
                      .newInstance();
            return operation.perform(a, b);
        }
        catch (Exception e) {
            throw new IllegalStateException(
                "Error instantiating operation", e);
        }
    }
}
  
public class Main {
    public static void main(String[] args)
    {
        // Create calculator object
        Calculator calculator = new Calculator();
        // Registering operations
        calculator.registerOperation("add",
                                     AddOperation.class);
  
        String operationName = "add";
        double a = 7, b = 9;
        try {
            double result = calculator.performOperation(
                operationName, a, b);
            System.out.println("Result: " + result);
        }
        catch (IllegalArgumentException e) {
            System.out.println("Unknown operation: "
                               + operationName);
        }
        catch (IllegalStateException e) {
            System.out.println(
                "Error instantiating operation: "
                + e.getMessage());
        }
    }
}
  
// Class that implements the CalculatorOperation interface
// to perform addition
class AddOperation implements CalculatorOperation {
    @Override public double perform(double a, double b)
    {
        return a + b;
    }
}


Output

Result: 16.0
  1. The Calculator class uses a Map to store the registered operations. Each operation is represented by a class that implements the CalculatorOperation interface.
  2. The registerOperation method adds a new operation to the calculator by specifying the name of the operation and the class that implements it.
  3. The performOperation method is used to perform a specific operation. It uses Java’s reflection API to dynamically instantiate the operation class and call its perform method. This allows developers to add new operations to the calculator by simply providing a new class that implements the CalculatorOperation interface.
  4. This plugin system allows the code to be compiled once, and new functionality to be added at runtime while maintaining a consistent interface. This makes the code more flexible and easy to maintain.


Last Updated : 02 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads