Explaination
- Portability refers to the ability to run a program on different machines.
- Running a given program on different machines can require different amounts of work (for example, no work whatsoever, recompiling, or making small changes to the source code).
- When people refer to Java applications and applets as portable, they usually mean the applications and applets run on different types of machines with no changes (such as recompilation or tweaks to the source code).
Three Types of Java Portability :
Java as a language: source code portability
- As a programming language Java provides the simplest and most familiar form of portability -- source code portability.
- A given Java program should produce identical results regardless of the underlying CPU, operating system, or Java compiler.
- This idea is not new; languages such as C and C++ have provided the opportunity for this level of portability for many years.
- However, C and C++ also provide numerous opportunities to create non-portable code as well.
- Unless programs written in C and C++ are designed to be portable from the beginning, the ability to move to different machines is more theoretical than practical.
- C and C++ leave undefined details such as the size and endianism of atomic data types, the behavior of floating-point math, the value of uninitialized variables, and the behavior when freed memory is accessed.
- This semantic looseness allows a single block of C or C++ source code to compile to programs that give different results when run on different CPUs,
- Java is different. Java provides much more rigorous semantics and leaves less up to the implementer.
- Unlike C and C++, Java has defined sizes and endianism for the atomic types, as well as defined floating-point behavior.
Java as a virtual machine: CPU portability
- Most compilers produce object code that runs on one family of CPU (for example, the Intel x86 family).
- The current Java compilers are different. Instead of producing output for each different CPU family on which the Java program is intended to run, the current Java compilers produce object code (called J-code) for a CPU that does not yet exist.
- For each real CPU on which Java programs are intended to run, a Java interpreter, or virtual machine, "executes" the J-code. This non-existent CPU allows the same object code to run on any CPU for which a Java interpreter exists.
- The benefit to compiling programs (in any language) to J-code is that the same code runs on different families of CPUs.