Sashi Kant
Sashi Kant

Reputation: 13465

Platform independence in Java's Byte Code

I sometimes wonder why Java is referred as a Platform Independent Language?

I couldn't find a proper explanation of the below points :

  1. Is the JVM same for Windows/Linux/Mac OS?
  2. Are the bytecode generated same for a same Class in the above environments?

If the answer to the above questions are NO then how the platform independence is achieved.

Please help me out in learning this basic concept.

Upvotes: 4

Views: 7396

Answers (7)

prime
prime

Reputation: 15564

Two things happen when you run an application in Java,

  • Java compiler (javac) will compile the source into a bytecode (stored in a .class file)

    The java Byte Code (.class) is OS independent, it has same extension in all the different OSs. But since this is not specific to any OS or other environment no one can run this (Unless there is a machine whose native instruction set is bytecodes, i.e. they can understand bytecode itself)

  • JVM load and execute the bytecode

    A virtual machine (VM) is a software implementation of a machine (i.e. a computer) that executes programs like a physical machine. Java also has a virtual machine called Java Virtual Machine (JVM).

Java Code Execution Process

JVM has a class loader that loads the compiled Java Bytecode to the Runtime Data Areas. And it has an execution engine which executes the Java Bytecode. And importantly he JVM is platform dependent. You will have different JVM for different operating systems and other environments.

The execution engine must change the bytecode to the language that can be executed by the machine in the JVM. This includes various tasks such as finding performance bottlenecks and recompiling (to native code) frequently used sections of code. The bytecode can be changed to the suitable language in one of two ways,

Interpreter : Reads, interprets and executes the bytecode instructions one by one

JIT (Just-In-Time) compiler : The JIT compiler has been introduced to compensate for the disadvantages of the interpreter. The execution engine runs as an interpreter first, and at the appropriate time, the JIT compiler compiles the entire bytecode to change it to native code. After that, the execution engine no longer interprets the method, but directly executes using native code. Execution in native code is much faster than interpreting instructions one by one. The compiled code can be executed quickly since the native code is stored in the cache.

So in a summary Java codes will get compiled into a bytecode which is platform independent and Java has a virtual machine (JVM) specific to each different platforms (Operation systems and etc) which can load and interpret those bytecodes to the machine specific code.

Refer :

Upvotes: 1

RishiKesh Pathak
RishiKesh Pathak

Reputation: 2272

1 ) Is the JVM same for Windows/Linux/Mac OS?

Answer ===> NO , JVM is different for All

2 ) Are the bytecode generated same for a same Class in the above environments?

Answer ====> YES , Byte code generated will be the same.

Below explanation will give you more clarification.

{App1(Java code)------>App1byteCode}........{(JVM+MacOS) help work with App1,App2,App3} {App2(Java Code)----->App2byteCode}........{(JVM+LinuxOS) help work with App1,App2,App3} {App3(Java Code)----->App3byteCode}........{(JVM+WindowsOS) help work with App1,App2,App3}

How This is Happening ?

Ans--> JVM Has capability to Read ByteCode and Response In Accordance with the underlying OS As the JVM is in Sync with OS.

So we find, we need JVM with Sync with Platform.

But the main Thing is, That the programmer do not have to know specific knowledge of the Platform and program his application keeping one specific platform in mind.

This Flexibility of write Program in Java Language --- compile to bytecode and run on any Machine (Yes need to have Platform DEPENDENT JVM to execute it) makes Java Platform Independent.

Upvotes: 2

Aatul Palandurkar
Aatul Palandurkar

Reputation: 21

Watch this 2 min video tutorial hope this will help you understand that why java is platform independent? Everything is explained in just 2 min and 37 seconds.

Why Java is platform independent? https://www.youtube.com/watch?v=Vn8hdwxkyKI

And here is explanation given below;

There are two steps required to run any java program i.e. (i) Compilation & (ii) Interpretation Steps.

Java compiler, which is commonly known as "javac" is used to compile any java file. During compilation process, java compiler will compile each & every statement of java file. If the java program contains any error then it will generate error message on the Output screen. On successful completion of compilation process compiler will create a new file which is known as Class File / Binary Coded File / Byte Code File / Magic Code File.

Generated class file is a binary file therefore java interpreter commonly known as Java is required to interpret each & every statement of class file. After the successful completion of interpretation process, machine will generate Output on the Output screen.

This generated class file is a binary coded file which is depends on the components provided by java interpreter (java) & does not depends on the tools & components available in operating system.

Therefore, we can run java program in any type of operating system provided java interpreter should be available in operating system. Hence, Java language is known as platform independent language.

Upvotes: 1

Ajay Bhojak
Ajay Bhojak

Reputation: 1159

Machine Dependence: This means that whatever you want to execute on your hardware architecture will not be able to execute on another architecture. Like If you have created an executable for your AMD architecture it will not be able to run on Intel's architecture. Now comes Platform Dependence is that you have created some executable for your Windows OS which won't be able to run on Linux.Code written in Assembly(provided by your processor) or Machine Language are machine dependent but if you write code in C,CPP,JAVA then your code is machine independent which is provided by underlying OS.

Platform Independence:If you create some C or CPP code then it becomes platform dependent because it produces an intermediate file i.e. compiled file which matches to the instruction set provided by underlying OS. So you need some mediator which can understand both compiler and OS.Java achieved this by creating JVM. Note: No language is machine independent if you remove the OS which itself is a program created using some language which can directly talk to your underlying machine architecture. OS is such a program which takes your compiled code and run it ontop of the underlying architecture.

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

Is the JVM same for Windows/Linux/Mac OS?

Not at all. Compiler is same across the platforms. But, since it is an executable file, the file itself will be different i.e. on Windows, it would be .exe, on Linux, it would be Linux executable etc.

Are the bytecode generated same for a same Class in the above environments?

Yes. That is why Java is COMPILE ONCE. RUN ANYWHERE.

enter image description here

Before starting please read this doc by oracle

Upvotes: 9

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

Java is called a plattform indipendent language, because virtually all you need to run your code on any operating system, is that systems JVM.

The JVM "maps" your java codes commands to the system's commands, so you don't have to change your code for any operating system, but just install that system's JVM (which should be provided Oracle)

The credo is "Write once, run anywhere."

Upvotes: 1

Patashu
Patashu

Reputation: 21773

The meaning of platform independence is that you only have to distribute your Java program in one format.

This one format will be interpreted by JVMs on each platform (which are coded as different programs optimized for the platform they are on) such that it can run anywhere a JVM exists.

Upvotes: 2

Related Questions