One of the frequently asked questions in a programming interview is “Why is Java a portable language?”. Java is considered a portable programming language, mainly because a generic Java Application can be run from most machines. In the next chapters, we are going to show the reasons that made possible this feature.
Compilation phase
The source code of a Java application is written inside files with the .java extension.
For example, a simple “hello world” program could have a source code saved as HelloWorld.java
The file in the above figure, represents the program’s source code, and as such cannot be executed by a computer. For this purpose, it must first be compiled.
As shown in the figure, compilation process by Java compiler, generate a second file named HelloWorld.class: the name is the same as the original one, but the extension is different (now is .class ).
HelloWorld.class is the actual application and as such can be run from a machine; we will see how in the next paragraph.
HelloWorld.java contains Java statements written by the programmer, but if we open HelloWorld.class with a text editor, a lot of strange symbols will appear.
That’s because HelloWorld.class contains bytecode statements, a “translated” version of the Java sourcecode. Bytecode is the JVM programming language.
Interpretation phase
JVM, also called Java interpreter, is the Java Virtual Machine. After recognizing the bytecode statements inside the HelloWorld.class file, the JVM can run it.
The same HelloWorld.class application can be run on any machine with a JVM. This is true for any Java application, which is the reason of the original question : Why is Java a portable language?
References
https://en.wikipedia.org/wiki/Java_virtual_machine
http://marcoparoni.altervista.org/
See also…
http://marcoparoni.altervista.org/maven-dependency-management/