Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
PrintWriter write(char[]) method in Java with Examples
Next article icon

Why is Java ‘write once and run anywhere’?

Last Updated : 26 May, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report

JVM(Java Virtual Machine) acts as a run-time engine to run Java applications. JVM is the one that actually calls the main method present in Java code. JVM is a part of the JRE(Java Runtime Environment).

Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one system and can expect it to run on any other Java-enabled system without any adjustment. This is all possible because of JVM.

How Java is WORA:

In traditional programming languages like C, C++ when programs were compiled, they used to be converted into the code understood by the particular underlying hardware, so If we try to run the same code at another machine with different hardware, which understands different code will cause an error, so you have to re-compile the code to be understood by the new hardware.

In Java, the program is not converted to code directly understood by Hardware, rather it is converted to bytecode(.class file), which is interpreted by JVM, so once compiled it generates bytecode file, which can be run anywhere (any machine) which has JVM( Java Virtual Machine) and hence it gets the nature of Write Once and Run Anywhere.

Example: Practical Implementation of WORA using a simple JAVA program to check whether a number is even or odd.




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");
    }
}
 
 
  • For Compiling (done on Windows 10):
      javac GFG.java  
  • After compilation there will be a class file in the corresponding folder named as:
      GFG.class  
  • When copied the bytecode (.class) generated on compilation to a macOS 10.14.3 and running it we get the following output.

    Java program Compiled on WIndows and ran in macOS

Conclusion:
To sum it up, Java, when compiled, creates a bytecode (.class file), which can be run in any machine which supports JVM. So once compiled it doesn’t require re-compilation at every machine it runs, JVM converts the bytecode to be understood by the underlying hardware.



Next Article
PrintWriter write(char[]) method in Java with Examples
author
avi122186
Improve
Article Tags :
  • Java
  • java-basics
Practice Tags :
  • Java

Similar Reads

  • Writer append(char) method in Java with Examples
    The append(char) method of Writer Class in Java is used to append the specified char value on the writer. This char value is taken as a parameter. Syntax: public void append(char charValue) Parameters: This method accepts a mandatory parameter charValue which is the char value to be appended on the
    2 min read
  • Try-with-resources Feature in Java
    In Java, the Try-with-resources statement is a try statement that declares one or more resources in it. A resource is an object that must be closed once your program is done using it. For example, a File resource or a Socket connection resource.  The try-with-resources statement ensures that each re
    4 min read
  • Writer hashCode() method in Java with Example
    The hashCode() method of Writer Class in Java is used to get the HashCode value of this Writer instance. This method does not accepts any parameter and returns the required int value. Syntax: public int hashCode() Parameters: This method accepts does not accepts any parameter. Return Value: This met
    2 min read
  • PrintWriter write(char[]) method in Java with Examples
    The write(char[]) method of PrintWriter Class in Java is used to write the specified character array on the stream. This character array is taken as a parameter. Syntax: public void write(char[] charArray) Parameters: This method accepts a mandatory parameter charArray which is the character array t
    2 min read
  • CharArrayWriter write() method in Java with Examples
    The write() method of CharArrayWriter class in Java is of three types: The write(int) method of CharArrayWriter class in Java is used to write a character to the writer in form of an integer. This write() method writes one character at a time to the CharArrayWriter.Syntax: public void write(int c) O
    3 min read
  • Private and Final Methods in Java
    Methods in Java play an important role in making the code more readable, support code reusability, and defining the behaviour of the objects. And we can also restrict the methods based on requirements using the keywords and modifiers such as final and private. These two have different, distinct purp
    5 min read
  • CharArrayWriter writeTo() method in Java with Examples
    The writeTo(Writer) method of CharArrayWriter class in Java is used to write the contents of the CharArrayWriter to another character stream.Syntax: public void writeTo(Writer out) throws IOException Parameters: This method accepts one parameter out that represents the output stream that is the dest
    1 min read
  • Writer append(CharSequence) method in Java with Examples
    The append(CharSequence) method of Writer Class in Java is used to write the specified CharSequence on the writer. This CharSequence value is taken as a parameter. Syntax: public Writer append(CharSequence charSequence) Parameters: This method accepts a mandatory parameter charSequence which is the
    2 min read
  • Different name reusing techniques in Java
    Overriding An instance method overrides all accessible instance methods with the same signature in superclasses, enabling dynamic dispatch; in other words, the VM chooses which overriding to invoke based on an instance’s run-time type. Overriding is fundamental to object-oriented programming and is
    3 min read
  • PrintWriter write(String) method in Java with Examples
    The write(String) method of PrintWriter Class in Java is used to write the specified String on the stream. This String value is taken as a parameter. Syntax: public void write(String string) Parameters: This method accepts a mandatory parameter string which is the String to be written in the Stream.
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences