Application of Adapters - Java Architecture

Application of Adapters - Java Architecture

I am sure most of the readers would be aware of a programming language called “Java”.

Now can you think about the uniqueness of Java as a programming language which had made it one of the most successful programming languages.

I hope most of us will agree java as a programming language is known for platform independence.

Now what can we understand by the term “Platform independence” ? When I ask this question most of the times I get very confused and contradictory answers from my participants.

When we say “Platform Independence” .. this independence should be relative to something … What is this something?

The punch line of Java says “Write Once Execute Anywhere …”

What do we write once …?

I hope it is the application program. The punch line of java means “Write the application program once and execute it on any of the hardware platforms.”

So let us understand this independence is relative to the application programs.

We also know that Java and C++ are both high level programming languages but deep down everything gets converted into machine instructions which is specific / native to different hardware platforms.

So why is java as a programming language considered as Platform Independent while C++ as a programming language considered platform dependent?

Does it mean that while we are writing C++ application programs, the way we write the application program (syntax) is dependent on the hardware platform on which it will execute? The answer is yes.

I don’t know whether most of the readers are aware of the same, but let us understand the way we write the syntax differs depending upon whether the program will execute on the 8 bit , 32 bit, 64 bit or 128 bit machine. Hence in C++ the way we write the application program is dependent on the hardware platform on which it will execute. This is the reason C++ is considered platform dependent.

While when we write an application program in java we don’t bother about the hardware platform or the architectural style of the machine irrespective of whether it’s a 8 bit or 16bit or a 32 bit machine… This is the reason java is considered to be platform independent.

I am sure most of the readers will realize that platform independence can also be understood as flexibility of application programs wrt hardware platforms.

Look into the next figure …. if you want to increase the flexibility of the application programs wrt the hardware platforms … how would you do it ?

Figure- Figure

Generally how do we increase the flexibility of any system?

Introduce a level of indirection.. and that’s what was done to make Java Platform Independent.

So can we guess… what was the level of indirection introduced to increase the flexibility wrt the hardware platforms? Many of my participants say … the JVMs …but the actual answer is the .Class files .

The .Class files is the level of indirection used to increase the flexibility of the application programs wrt hardware platforms.

Let us quickly look into the critical elements of the Java Architecture and understand how flexibility is provided wrt the platforms. The following figure shows us the same.

Figure- Figure

The application programs in Java are written in a .java file. The program is then supplied to the compiler which checks the syntax of the .java file for syntactic correctness. If the syntax is found to be correct the .class file is generated. The .class file contains bytecodes which is a platform neutral way of specifying the machine instruction set. The .class file is line an interface which can be implemented in multiple ways .But let us understand that the hardware platforms cannot understand the bytecodes they can only understand their native instruction set which is specific to their hardware architecture style. So don’t you think we need some kind of adapters who knows how to convert a standard instruction set into the native instruction set specific to the corresponding platform. The JVMs are the adapters used to do this adaption.

Think about it, Sun came out with one of the most successful programming languages just by using 3 fundamental design principles …. Level of indirection [.class file] for flexibility, Polymorphism and Adapters.

Now comes a very interesting question …

Who writes the JVM… whether Sun writes the JVM or whether the hardware platform vendors the JVM?

This question is interesting because if I ask this question to the engineers who are not working on Java J2EE, their answer is the hardware platform vendors while if I ask the same question to engineers who are working on Java J2EE their answer is Sun writes the JVM.

What do you think about it?

The right answer is .. the hardware resource provider writes the JVM or the adapter for their platform.

Think about it how can Sun write the adapter/JVM for a particular platform? For Sun to write the adapter … Sun has to understand the native instruction set of the particular hardware platform … now ask your self … don’t you think that the hardware platform itself is a blackbox and will never expose its internal details to the outside world. So technically speaking the hardware platform vendors will never expose their internal details without which sun can never write the JVM or the adapter.

Let us consider a hypothetical case that the hardware platform vendors exposes its internal architecture and its details for the Sun to write the JVM. In this case every time any of the hardware vendors changes their architectural style or the native machine instructions, Sun will have to keep tracking these changes and keep modifying the JVMs accordingly. Do you feel sun will take this headache of modifying itself for every other change happening …? Is it a flexibile design?

I hope you will agree … from both the perspectives and logically as well the adapters will always have to be provided by the resource providers as they know their internal details well.

So why did engineers start thinking that JVMs are provided by Sun? This is just because they download it from the Sun Site but just because we download it from the sun site doesn’t mean that Sun is writing the same as well. This was just done for the convenience to ensure that people don’t have to look around for different JVMs for different platforms across different vendors site.

This section will help us understand one of the most important design principles “Designing is all about simplicity” or “Some of the most successful systems, products or frameworks are not successful because someone thought very complex, they were successful because someone thought so simple that others in the world were not able to think.”

Just by using three basic principles, level of indirection, polymorphism and adapters … Sun created one of the most beautiful and successful programming language.

Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer