Skip to main content

Platform Independency

Java is always told to be platform independent. Which means, the code is written just once but can be executed on any platform.

How's independence achieved?

This comes from the 'byte code' which java generates. It's an intermediate format of the code that can be understood only by the JVM. JVM then does the two things to achieve platform independence:

  1. Every byte code has a platform specific native code mapping.
  2. Or complete bytecode is compiled to a full native code which is platform specific.
JIT compiler

Just-in-Time compiler of Java means how the JVM converts the byte code into native code during the execution itself.

See code execution to understand what JIT compilation exactly is.

Example

If we write a file open code in Java, then the file handling code is automatically converted to Windows or Linux based on the platform on which the JVM is running. There is no special handling needed by the developer to handle different platforms.

JRE contents

The only thing that JRE contains is the compiled version of built-in libraries.

only JVM is platform dependent

Only JVM has platform dependent implementation. But since JRE contains the JVM and JDK contains JRE, even they become platform dependent.

This is why when you download JDK, you must specify the platform too.

JVM-JRE-JDK