PTK
PTK

Reputation: 323

How does Java solve portability?

The Java compiler converts Java code to bytecode and then the JVM converts the bytecode to machine instructions. As far as I have understood, JVMs are built for different platforms (processor + OS). Then how can we say that Java is platform independent? Ultimately, we require a JVM which is platform dependent?

Upvotes: 0

Views: 472

Answers (2)

Khanna111
Khanna111

Reputation: 3913

The end-user writes code in Java and that is platform independent.

The JVM engineers work on creating the JVM and the JRE and within it the compiler and the interpreter for different platforms. Therefore that absolves the end-user to worry about porting their codebase to different platforms. Write once and then run it on all platforms (as long as there is a JVM available for that platform).

So from the perspective of the end-user (Java programmer) the Java code is indeed platform independent since it can run anywhere without any changes. Even though the JVM is ported to different platforms and is indeed platform dependent.

Difference with C/C++

As it is with the JVM, there exists a C/C++ compiler for each platform responsible for translating source to machine/processor instructions. These machine instructions are understood by the processor, for instance: Intel etc. Therefore a C/C++ compiler is made available for each platform it supports. However, you might need to link to specific OS/platform specific libraries in your program. That will make your program platform specific and will need to be rewritten for another platform by linking to that platform's version of that library. That becomes the C/C++ programmer responsibility. Also each C/C++ compiler has its own vagaries specific to a platform unlike a JVM which provides a consistent view across platforms detailed below.

In case of Java, you are removed from tapping into platform specifics (unless the JVM or Java libraries do not provide it but they usually cover most of the basics). The Java code taps into JVM and Java libraries and the JVM is responsible for translating that to bytecode or to machine instructions (to improve performance) at runtime. Unless you use JNI in your Java sources, you do not have to worry about portability. For instance: within your Java program, you can access the native environment, the host memory, some OS characteristics etc., and your code is still portable. The JVM provides the infrastructure in that the native calls to extract that information specific to the platform. However there might be some platform specific functionality that your Java program requires that is not provided by the JVM or Java libraries and therefore you need to use JNI and your portability suffers as a result.

Note: C/C++/Java all have source code portability. But what the JVM provides is binary portability. The compiled or interpreted java code runs on an abstraction called the JVM (Virtual Machine). The java programmer writes to that interface instead of the platforms that the JVM runs on. There is a clear separation here.

Upvotes: 1

UkFLSUI
UkFLSUI

Reputation: 5672

As you have mentioned yourself, the JVM is platform-dependent. That's it - the JVM is platform-dependent, but not Java. After all, the JVM needs to be run in someway inside the native machine, so it must have to be specific to that platform.

Java is portable in the sense that the compiled code is portable. For example, if you compare with C, both the C and Java source codes are portable, that means both of them provide source code portability. Once you have a source code written in a Windows PC, you can transfer that exact code in another Linux machine and both Java and C code will compile and run fine in both machines.

But, what about object code portability?

We know, when we compile a C code, it produces the machine readable object code. So, if you compile a C source code from one machine, then that object code may not be run from another machine if they are not compatible. But, in case of Java, if you compile a Java source code to bytecode from one machine, that bytecode can be run in any machine that runs the JVM.

Another interesting fact, by successful compilation of a Java source code, we are also producing a bytecode for some unknown future CPU which doesn't even exist as JVM acts as a kind of virtual CPU.

Upvotes: 1

Related Questions