Introduction to How Java is Platform Independent Language
An algorithm is a verbal or written description of a logical sequence of actions applied to the object.
Translation of a problem into a workable solution is called an algorithm and translating algorithms into a programming language is called coding.
Collectively coding and testing the solution for better quality is referred to by the implementation.
A successful implementation if we need to modify the code(due to anything-requirement change, bug fix, new defect), we call it maintenance.
Machine code
Machine codes are made up of binary codes. A computer can understand machine code. All instructions and data first transferred into binary codes and feeds into the computer. These programs are not human-friendly. They are tough to read and modify.
Assembly language:
Assembly languages are one step above machine code where programmers are easy to remember. Binary code is referred to as mnemonic.
Machine Language:100101
Assembly code-ADD
A program that translates assembly code to equivalent machine code is called Assembler. And a compiler is a program that can translate a High-level language to machine code or object code.
If we look at the programming languages, they can be divided into three different segments.
- Low-level programming language
- Machine level programming
- Assembly level programming.
If programs are written in assembly language or machine language, they cannot be portable as they depend on underlying machine instruction.
High-level programming language
High-level programming languages are further upgraded languages that can take human-like language and transform them into machine code. Most high-level languages are machine-independent or portable. The algorithm of the problem, data type specifications, and instructions are written in a high-level programming language.
- it is procedural-based like c, Pascal, Fortran COBOL, etc. procedural programming manipulates or works with the data with procedures.
- Functional programming -LISP
- Logic level programming -Prolog
- Object-Oriented Programming-c++,Smalltalk,Java.
Very high-level programming language
- Command language-DCL, Shell programming,
- Query based-SQL
When a low-level programming language is very tedious to write code, the high-level, and very high-level programmings are coder-friendly.
In low and high-level coding user need to specify how to solve a problem whereas, in the very high-level coding language, the user needs to specify what to solve.
As data is the basic unit block in these languages, they can be called data-oriented languages. The microprocessor inside every computer can understand and execute only machine code i.e the binary zero (0) and one (1).
So there is a mechanism that converts the high and very level code (written in English) to lower-level machine code which is relevant to the microprocessor.
As machine codes vary from the microprocessor to microprocessor, the same source code should be translated to different microprocessors using different translators.
So a source code converted to machine code for Intel chip will differ from Motorola chip or to AMD Athlon chip. The problem does not end here.
The same problem exists in the case of the same microprocessor with a different operating system or with the same operating system, the same microprocessor with different bit configuration x84,32 bit and x64 or 64 bits.
How does Java get the job done?
A Java application is compiled to Java bytecode which is the instruction set for Java Virtual Equipment (JVM). So a Java program is translated to a standard machine language called Bytecode.
Java compiler does not translate Java codes immediately to device code alternatively it first interprets the Java application to BYTECODE which is effectively an item file for a virtual machine that makes use of the extension.class This byte-code is established for a virtual system known as the Java Digital Equipment or JVM.
The bytecode verifier in the JVM then checks the bytecode immediately after which the JVM converts the bytecode to an equipment-welcoming code.
To be much more particular it is the Just In Time (JIT) compiler of the JVM which is accountable for this conversion.
It also retains the observation of the commonly repeated byte-codes so as not to repeat the compilation of identical codes yet again and yet again.
So Java virtual machine translates the bytecode to actual machine code as per the specific computer architecture.
This is how Java capabilities. As the JVM converts the bytecode to a device unique code, for that reason we have distinctive JVMs for unique Java platforms mainly because JVMs are not system independent, the platform independence that Java demonstrates is a layer of abstraction, less than which dependence on the system in fact exists.
The components used in conversion are:
- Assembler-Converts High-level programming into low-level machine language.
- Compiler- Divides the high-level program into a small chunk of codes.
The general execution process:
High level code-> compiler->small chunk of code blocks->assembler->equivalent machine code or native code->processor->compute->result
Compiler design is an expensive process and each operating system interprets the command differently. So the compiled code in one machine may not run in another machine or in another operating system.
Java has come up with a new concept called byte code which is the third stage in traditional program execution. JVM (Java virtual machine) converts the compiled code to a third stage called byte code. (a class file)
The program execution for Java is done as below:
java source(.java file)->compiler->.class file-> JVM-> Native machine code based on the execution environment->result
Execution environment can be any combination:
- Intel+windows
- AMD+Linux
- Intel+Linux etc
This is why Java can be written once and run anywhere, anytime on any machine configuration (microprocessor, operating system, bit configuration). In Java, it is written once but can be executed multiple times. The bytecode can be available for further consumption over the internet.
As we discussed, the byte code can be saved for future use. The bytecode can be used again and again without recompiling the same source code.
A computer can easily execute a program that is already compiled and transformed into machine code. However, the computer can not directly consume and execute the bytecode produced by the java compiler. It must execute JVM to get the equivalent machine language instruction from the bytecode.
JVM (Java virtual machine) is a software program that resides in the main memory(RAM) of the corresponding operating system. When we feed the .class file(byte code), it gets activated, detects the target platform, and converts the .class file (byte code) to equivalent native or machine code which the processor can understand and execute.
Unlike any other language compiler (VB, C, etc) Java compiler does not produce native executable code for a particular machine. It produces its own format .class file which is nothing but a byte code, then feeds the same into the target JVM(Java virtual machine).
JVM(Java virtual machine) converts the byte code from Native executable code. The compiler-generated byte code is understood by JVM (Java virtual machine) only.
JRE(Java runtime environment) has a part called JVM(Java virtual machine). JVM has three parts:
- ClassLoader
- Byte Code Verifier
- Executable Engine
Why Java is Both Interpreted and Compiled Language?
A compiler converts higher programming language to machine-level code. In the case of java, the compiler converts the high-level English-like language to byte code -a flavor of machine-level code.
An Interpreter converts one language to another, at the same level as a code interpreter from C++ to Java. In Java JIT(Just In time) code generator converts low-level byte code to native machine code. Both are at the same level.
Source: http://stackoverflow.com/questions/3618074/what-is-the-difference-between-compiler-and-interpreter
Example of a high-level language to machine level conversion
Let us take an example of a program written in the hypothetical language which prints the square of 20 on the screen:
High-level language
x = 20
y = x*x
print y
If this program is given to a compiler for this hypothetical language, then the compiler would first convert the program into assembly such as
mov ax,20
mov bx,ax
mul bx,bx
push bx
call _printCompiler and interpreter Bird’s eye view on how they work
This is why Java is called both compiled and interpreted language at the same time. It has an important consequence on the speed. This is why Java is also as well.
Dynamic linking –It links each time we execute java code.
Run time Interpreter: The conversion of byte code into machine code is done at run time. These are the two main reasons why Java is slower than other high-level languages.
However, Java has tried to overcome the problem of architecture and environment dependency by introducing JVM(Java Virtual Machine).JVM specifies a detailed dummy CPU and instruction set that looks exactly like standard assembly code. The java compiler transforms the source program into the instruction set of JVM.
The compiled Java source code is known as byte code instruction. Byte code is nothing but another assembly code. Next, the Java interpreter which is specific to each environment(process, OS, bit) converts the byte code into native processor instruction before executing it.
Execution of Java:
java source(.java file)->compiler->.class file-> JVM-> Native machine code based on the execution environment->result
Thus while converting source code into Object code, compiling is carried out halfway and the interpreter does the remaining half. Java byte codes are architecture and Operating system independent. They are highly portable and can be executed on any system without any change.A byte code verifier in java interprets the execution of incoming byte codes and verifies whether the compiled code is strictly language complaint. In case it finds any illegal code, the JRE (Java runtime environment) rejects the code and refuses to execute it.
Source LanguageTranslator Target Language LaTeX Text Formater PostScript SQL database query optimizer Query Evaluation Plan Java javac compiler Java byte code Java cross-compiler C++ code English text Natural Language Understanding semantics (meaning) Regular Expressions JLex scanner generator a scanner in Java BNF of a language CUP parser generator a parser in Java Source: http://lambda.uta.edu/cse5317/notes/node3.html
Execution strategy Languages Bytecode interpreter UCSD and Berkeley Pascal, JavaScript, Java ≤1.2, Python, Ruby 1.9, Smalltalk-80, Visual Basic ≤4 Tree-based interpreter Perl ≤5, Ruby ≤1.8 Native code compilation C, C++, Visual Basic 5 and 6 Bytecodes + dynamic compilation Java ≥1.3, C#, Self, Smalltalk, Visual Basic .NET Source: http://www.antlr.org/wiki/display/ANTLR3/The+difference+between+compilers+and+interpreters
The JVM architecture is given as:
ClassLoader:
It loads a class and performs loading, linking, and initialization.
Inside Java Virtual Machine, there are below segments:
Method Area:
Store class structure like metadata.The constants run-time pool and the code for methods.
Heap:
All objects their related instance variables and arrays are stored inside the heap. The memory is common and shared across multiple threads.
JVM Language Stack:
stacks the local variables and partial results. Each thread has its own JVM stack, created simultaneously as the thread is created. The frame is created whenever a method is invoked and the frame is deleted when execution is completed.
PC Register:
It stores the address of the Java virtual machine instructions which is currently executing. In Java, each thread has its separate PC register.
Native method stack:
It holds instruction of native code depends on the native library. It is written in some other language, other than Java.
Execution Engine:
This is a complete system to test hardware-software and the complete system. It never carries any information about the tested products.JIT is a part of JVM that interprets part of the byte code that has similar functionalities at the same time.
To remember easily, Java does not provide the machine language code directly. This makes Java portable but it introduces a slow execution of the code.