WORA write once and run anywhere nature java

To call the main method present in Java code, the JVM(Java Virtual Machine) is used which is a part of the JRE(Java Runtime Environment) and acts as a run-time engine to run Java applications. Because of JVM, a Java code can be developed on one system and can be expected to run on any other Java-enabled system without any adjustment, i.e, the Java applications are WORA (Write Once Run Anywhere).

How Java is WORA:

C or C++ being a traditional programming language converts the program into the code understood by the particular underlying hardware when compiled. The code needs to be recompiled to be understood by the new hardware in case we have to run the same code at another machine with different hardware. Otherwise, the hardware which understands different code will cause an error.

In Java, the program is converted to bytecode(.class file) instead of not converting it to a code directly understood by Hardware. The bytecode is interpreted by JVM. The bytecode file generated after the code compilation can be run anywhere or on any machine with JVM (Java Virtual Machine). Thus, Java holds the behavior of WORA (Write Once Run Anywhere).

Example:

import java.util.Scanner; 
 
class GFG { 
    public static void main(String args[]) 
    { 
        int num; 
        System.out.println("Enter a number:"); 
        Scanner input = new Scanner(System.in); 
        num = input.nextInt(); 
        if (num % 2 == 0) 
            System.out.println(num + " is even"); 
        else
            System.out.println(num + " is odd"); 
    } 
}

Output:

Explanation:

In the above example, we are demonstrating a practical implementation of WORA. Here, we are using a simple JAVA program to check whether a number is even or odd. A bytecode (.class) file is created when Java is compiled. This file can run on any machine with JVM. Thus, re-compilation is not required at every machine it runs, once compiled. The bytecode is converted by JVM to be understood by the underlying hardware.

For Compiling (on Windows 10):

javac GFG.java

A class file will be created in the corresponding folder after compilation and will be named as:

GFG.class

We can get the above output by running the copied bytecode (.class) generated on compilation to any OS.