Platform in-dependency in Java
A compiler is a software that converts the source code into a specific target language according to its design. It can be a machine code/native code or can be some other source code too. The process that a compiler is doing is called compilation.
Compiler responsibilities will be mainly
- Verifies the syntax & semantics of the programming language
- Optimizes the code
- Converts to the target language
The Target Language can be a Native(Machine) code or Intermediate Language or can be other programming Language.Ex: ‘C’ – After the compilation process produces Machine Code.‘Java’ – After the compilation process produces Intermediate code-named bytecode.
‘Clojure’ – After the compilation process produces Javascript, which is another programming language.
Compilers which convert one source to other language source are called source-to-source compilers or transcompilers or transpiler.
A programme compiled in the Linux platform can be executed only in Linux platform. And same is the case with Windows Platform compilation and execution process.
The main difference is because of the executable file formats in different platforms.Windows executable format file is PE – Portable Executable.
Linux executable format file is ELF – Executable Linkable Format.
Compiled programmes give faster execution but have platform dependency issues.
Interpretation
Interpreters will have their libraries of precompiled machine code for the individual instructions of the source. Its execution is almost similar to CPU’s fetch & executing process.
Each interpreter will have its platform-specific precompiled machine code library. So, interpreted languages don’t have platform dependency issues.
But in interpretation, fetching every time its libraries is costly memory access and the source code is reinterpreted for every execution causing slow execution.
Though interpreted languages solve platform dependency, their execution process is slow.
Java uses best of both compilation and interpretation
Java uses best of both compilation and interpretation
Java Source code is converted to bytecode by Java compiler. The platform-dependent JVM interprets this bytecode to produce the results of the programme.
Bytecode being compact, optimized and compiled, gives the faster execution.
JIT(Just In Time) Compiler identifies frequently executed code as ‘hotspots’. These hotspots are interpreted into Native Code and stored in Cache for faster execution process.