Java as a Machine Independent
Language
Introduction
Java is known for its platform independence, which means that a Java program can run on
any device or operating system that has the Java Virtual Machine (JVM) installed. This is one
of the key features that makes Java a powerful and versatile programming language.
Why Java is Machine Independent
Java achieves machine independence through the use of bytecode. When a Java program is
compiled, it is not compiled into machine-specific code. Instead, it is compiled into
bytecode, which is an intermediate, platform-independent code. This bytecode is then
interpreted by the JVM on any machine, allowing the same Java program to run on multiple
platforms without modification.
Activity: Demonstrating Machine Independence Using Notepad
Follow these steps to demonstrate Java’s machine independence:
1. Open Notepad on your computer.
2. Type the following Java program and save it as "HelloWorld.java":
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
3. Open Command Prompt (Windows) or Terminal (Mac/Linux).
4. Navigate to the directory where you saved HelloWorld.java.
5. Compile the program using the Java compiler:
javac HelloWorld.java
6. This will create a HelloWorld.class file (bytecode).
7. Run the program using the JVM:
java HelloWorld
8. You should see the output:
Hello, World!
9. You can now copy the HelloWorld.class file to any machine with a JVM installed and
run it using 'java HelloWorld' without recompiling.
Conclusion
This activity demonstrates how Java code, once compiled to bytecode, can run on any
machine with a JVM. This capability makes Java a truly machine independent language.