Saturday, November 11, 2017

Memory Representations of Class & Objects in Java vs Javascript

Java objects and Class based Inheritance in Memory:
In Java, the concept of Templates called Class exists. The main purpose is to tell how to create an object of a class if requested during runtime by using 'new ();'. 

As you know, a class can inherit another class by using 'extends' and on and on..we can have any number of levels of inheritance. Note that each class directly or indirectly extends from root class called 'Object'. That means every object in java has a direct or indirect reference to Object's object instance.

While creating objects for classes, each object, its parent's object and all along till the root(Object's object)...all have their own copies in the memory.

Consider the following inheritance:
ModelSX --> BMW --> Car --> Vehicle --> Object
ModelVClass --> Benz --> Car --> Vehicle --> Object

ModelSX modelSX1 = new ModelSX();
ModelSX modelSX2 = new ModelSX();



As shown above, each single object has its own hierarchical copies of objects. The same applies for ModelVClass and any other class.



JavaScript objects and Prototype based Inheritance in Memory:
Predominantly there are 4 ways to create objects and one of them is via constructor

Here is how:
function Car(model,number,type){
this.model = model;
this.number = number;
this.type = type;
}
var bmwSX1 = new Car("SX1",1234,"SUV");

Executing above lines of code, Javascript creates not just one but 3 objects - Car Function object, Car Function's object, Car's Prototype object. Car Function object & Car Function's object linked via Car's Prototype object. Similarly if you create an Employee object using an Employee function constructor, the 3-object set is created in memory. Like in Class based inheritance, there is a root 3-object set exists - Object Function object, Object Function's object & Object's Prototype. Below figure depicts link between root(Object prototype) and other user defined objects...



As shown above, if user tries to access a property on an object say employee, Jsengine first looksup at employee object, if it doesn't find then it goes to employee's prototype, and if it is still not succeed then it searches in Object's prototype.

How to establish multilevel hierarchy:
Say if we have two objects - Employee and Manager. In order to make employee as manager's parent, link manager's prototype's __proto__ property pointing to employee's prototype. Then the following inheritance will be achieved..
Manager-> Employee -> Object 

Thursday, November 2, 2017

Under the hood - what happens when a program is being run on a computer

A computer system is a bare minimum combination of - Software, Hardware and a bridge
Software: Applications and other programs
Hardware: CPU, Memory(RAM and ROM) and other hardware
Bridge: KERNAL

In present days, computer basically is a binary computer, well we are not yet into quantum-computing era. All the instructions fed to the processor consist of combinations of 1s and 0s. 1 and 0 are decoded to certain voltage levels in real world which will be passed to hardware via circuit.

Hence, at this point we say a processor is a microprocessor(an organized circuit of number of logical gates constructed with Transistors, Diodes etc) which is manufactured to be able to understand a set of instructions called "Instruction Set" specified by manufacturer.

Various processors(from different vendors Intel, AMD, Motorola, Apple etc) have their own instruction sets - x86, AMD64 etc)

Few sample instructions common across main stream sets below:
ADD, COMPARE, IN, JUMP, JUMP IF, LOAD, OUT and STORE etc. They are mnemonics which need to be fed in the form of binary digits.

Sample set of binary instructions or also called Machine Code:
ADD - 101001001001
SUBTRACT - 100000101001
COMPARE - 111100001101

Software: User application programs or System Programs
Any software program to be executed by a CPU need to have its source compiled to those Machine Code instructions which can be understood by the processor where it is supposed to be run. The system crashes if it encounters a foreign instruction. This is called Platform Dependence. 

*Platform - a Processor with its own Instruction Set

KERNAL:
A computing machine with its numerous built-in hardware resources need to be managed efficiently. Example - Memory management, Optimize performance, drive media and other hardware apart from CPU etc. This needs a software program called Kernal-normally written in C and/or Assembly - is the first program that is loaded by BootLoader when you switch-on a computer or a mobile. It has instructions to drive literally each and every hardware packaged in a computing system. As mentioned above, like any software program, Kernal needs to have its Machine code specific to the underlying Processor. Hence, Kernal developers should compile it to corresponding Instruction set of the CPU(platform dependency). Apart from managing system resources, Kenral has yet another important task of being a bridge between other software applications(programs) which need to be run on the machine.

Being a Bridge:
Lets say you have written a piece of Java Program which you want to run it. Assume that the program contains instructions not just to CPU(operations involved with Stack and Heap) but to interact with other hardware(I/O) like display, speakers etc. 

eg: System.out.println("Hello World"); --> This is the operation that should display the message on monitor. This is a H/W operation.

The .java program first compiles to ByteCode represented as .class file. A program called Java Virtual Machine(JVM - java.exe) interprets this ByteCode to Machine Instruction(remember specific to Instruction Set of the underlying Processor) which can be understood by CPU. As part of this process, all H/W operations get compiled to a code that make calls to Kernal's H/W invoking code(Machine Code) corresponding to the H/W you want to interact with. These calls are called 'System Calls' or 'OS Calls'. These system-call invoking operations are called Privileged means User applications don't have authority to directly invoke hardware but can only be done with the help of Kernal.

*java.exe: On a side note, this is too a compiled C/C++ program which can be directly executed by CPU.

When you give a command to run the .java program, the Kernal loads your compiled Machine Code in the memory(RAM) and instructs CPU to start acting on it. All the Stack & Heap related operations are directly executed by CPU but when it encounters any System Call, CPU executes corresponding Kernal's  Machine Code for that hardware. Hence, Kernal acts as a bridge between Hardware and software through System-Call architecture.

Lets understand with following example:
Example: Java program: Ignore the syntax for now.
int i=20;
int j=10;
sytem.out.println("Sum of i,j"+i+j);
System.playMusic("Some song");

Machine code of compiled Java program:
10010101 - CPU1 - Stack &Heap code
11010010 - CPU2 - Stack &Heap code
10100100 - MONITOR1 - System Call for Monitor
11010010 - CPU3 - Stack &Heap code
10010010 - MONITOR2 - System Call for Monitor
11010010 - CPU3 - Stack &Heap code
11010010 - CPU4 - Stack &Heap code
11100110 - SPEAKERS1 - System Call for Speaker
11010010 - CPU5 - Stack &Heap code
10101001 - SPEAKERS2 - System Call for Speaker

Kernal Machine Code for h/w resources:
1010100100101 - MONITOR1
1001001010010 - MONITOR2
1001010010100 - MONITOR3
0010010010010 - SPEAKER1
1001000000100 - SPEAKER2
0010100100100 - SPEAKER3

Finally, the combined code is what is being executed by CPU by switching between User's Java program and Kernal.
10010101 - CPU1
11010010 - CPU2
1010100100101 - MONITOR1
1001001010010 - MONITOR2
1001010010100 - MONITOR3
11010010 - CPU3
1010100100101 - MONITOR1
1001001010010 - MONITOR2
1001010010100 - MONITOR3
11010010 - CPU3
11010010 - CPU4
0010010010010 - SPEAKER1
1001000000100 - SPEAKER2
0010100100100 - SPEAKER3
11010010 - CPU5
0010010010010 - SPEAKER1
1001000000100 - SPEAKER2
0010100100100 - SPEAKER3


P.S.
MOST IMPORTANT: Any software program/application be it OS Program or User specific application - Java, c#, c++, c, JavaScript or python etc - has to be ultimately compiled to Machine Code corresponding to the specific CPU(Microprocessor) on which it needs to be run.

In fact, CPU doesn't different differentiate between OS program or User Application. It executes Binary Instructions of those programs.

CPU's view of Software applications or programs


Programmer's view of Software applications




How J2EE components work together in any Container - Spring or Application Server

In a Spring+Jersey+Hibernate RESTful webapplication, we can spot various J2EE components - JTA, JPA, Java Bean Validation, JSON-B API for B...