Platform Independence in Java Bytecode

I sometimes wonder why Java is referred to as a platform independent language?

I could not find the correct explanation for the following points:

  • Is the JVM the same for Windows / Linux / Mac OS?
  • Are bytecodes the same for the same class in the above environments?

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

Please help me learn this basic concept.

+4
source share
7 answers

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

Not at all. The compiler is the same on all platforms. But since it is an executable file , the file itself will be different, that is, on Windows it will be .exe, on Linux, it will be Linux executable, etc.

Are bytecodes the same for the same class in the above environments?

Yes. This is why Java COMPILE ONCE. RUN FORWARD

enter image description here

Read this document with oracle before starting

+8
source

Machine Dependency: This means that everything you want to do on your hardware architecture will not be able to do on another architecture. For example, if you created an executable file for your AMD architecture, it will not work in Intel architecture. Now Platform Dependence is that you created some kind of executable file for your Windows OS that will not be able to work on Linux. The code written in the assembly (provided by your processor) or machine language depends on the machine, but if you write code in C, CPP, JAVA, then your code does not depend on the machine that is provided by the base OS.

Platform independence: if you create C or CPP code, it becomes platform dependent because it creates an intermediate file, that is, a compiled file that matches the set of instructions provided by the base OS. So you need some kind of middleman that can understand both the compiler and OS.Java to achieve this by creating a JVM. Note. The language is machine independent if you uninstall the OS, which in itself is a program created using some language that can directly talk to your underlying machine architecture. OS is such a program that takes your compiled code and runs it on top of the underlying architecture.

+3
source

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

This one format will be interpreted by the JVM on each platform (which are encoded as different programs optimized for the platform on which they are installed) so that it can work anywhere in the JVM.

+2
source

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

Answer ===> NO, the JVM is different for everyone

2) Are byte codes the same for the same class in the above environments?

Answer ====> YES, The generated bytecode will be the same.

The explanation below will give you more explanation.

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

How does this happen?

Ans → JVM has the ability to read the byte code and respond in accordance with the underlying OS. Because the JVM is in sync with the OS.

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

But the main thing is that the programmer does not need to know specific knowledge about the platform and program its application, keeping in mind one specific platform.

This flexibility of the Java writer --- compile the bytecode and run it on any computer (you need to have Platform DEPENDENT JVM to execute it) makes an independent Java platform.

+2
source

Java is called a platinum independent language, because almost everything you need to run your code on any operating system is the JVM system.

The JVM "displays" your java code commands for system commands, so you do not need to change the code for any operating system, just install this JVM system (which should be provided by Oracle)

Credo - "Write once, run anywhere."

+1
source

Check out this video tutorial for 2 minutes, hope this helps you understand why Java is platform independent? Everything is explained in just 2 minutes and 37 seconds.

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

And here is the explanation below:

To run any java program requires two steps. (i) Compilation & (ii) Steps of interpretation.

The Java compiler, commonly known as "javac," is used to compile any java file. During the compilation process, the java compiler will compile each statement of the java file. If the java program contains any error, it generates an error message on the "Exit" screen. Upon successful completion of the compilation process, the compiler will create a new file called the File / Binary Coded File / Byte Code / Magic Code file.

The generated class file is a binary file, so a Java interpreter, commonly known as Java, is required to interpret each statement in the class file. Upon successful completion of the interpretation process, the machine will generate output on the output screen.

This generated class file is a binary-encoded file that depends on the components provided by the java interpreter (java) and does not depend on the tools and components available on the operating system.

Therefore, we can run a java program on any type of operating system if a java translator must be available on the operating system. Therefore, Java is known as a platform-independent language.

+1
source

Two things happen when running a Java application,

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

    The explicit bytecode (.class) is OS independent; it has the same extension in all different OSs. But since this does not apply to any OS or other environment, no one can start it (if there is no machine whose own set of commands are bytecodes, i.e. they can understand the bytecode itself)

  • JVM loading and bytecode execution

    A virtual machine (VM) is a software implementation of a machine (that is, a computer) that runs programs such as a physical machine. Java also has a virtual machine called Java Virtual Machine (JVM).

Java code execution process

The JVM has a class loader that loads compiled Java bytecode in the runtime data area. And it has a runtime engine that runs Java Bytecode. And importantly, the JVM is platform dependent. You will have a different JVM for different operating systems and other environments.

Execution mechanism should change the bytecode into a language that can be started by the machine in the JVM. This includes various tasks, such as finding performance bottlenecks and recompiling (on your own code) frequently used sections of code. The bytecode can be changed to a suitable language in one of two ways:

Interpreter: Reads, interprets, and executes byte code instructions one at a time

JIT Compiler (Just-In-Time): The JIT compiler was introduced to compensate for interpreter flaws. The execution mechanism is first started as an interpreter, and at the appropriate time, the JIT compiler compiles all the byte code to change it to its own code. After that, the execution mechanism no longer interprets this method, but directly executes it using its own code. Executing in native code is much faster than interpreting instructions one by one. Compiled code can be executed quickly because native code is stored in the cache.

So, in the summaries, Java codes will be compiled into bytecode, which is platform independent, and Java has a virtual machine (JVM) specific to each of the different platforms (operating systems, etc.) that can load and interpret these bytecodes are machine specific code.

Contact:

+1
source

All Articles