Java - The Beginning (Part 1: The Theory)
Tags: java, beginners, programming
Published: 2021-09-09 09:56:00

Hello everyone👋 I am Gaurav Gupta, an undergraduate student pursuing Computer Science from India. Before we dive into the blog, first I'd like to tell you about why this blog exists and what will be **Learnings Vault**.

### Why this blog exists

Ever since I started programming I always felt that I was lacking somewhere, and I keep discovering holes in my knowledge in many topics, sometimes the fundamentals aren't strong or other times it's some advanced topics. 

> 
 I write to discover what I know. - Flannery O'Connor

I wanted to write down my programming journey and learnings to identify what I know and where I can improve. This idea ended up here as a blog rather than a physical journal of my learnings because either I lose them or just never go back and revise them at regular intervals. Due to my personality, I constantly check on my posts or any activities that I have made public where people may see and judge me. This will at least force me to continuously go through my learnings, helping me to revise stuff. This has given birth to Learnings Vault.


❗❗**Note:** By no means, I am an expert at the topics I'll be writing in this blog, you can consider this as a beginner's point of view and understanding.


### What will be Learnings Vault?
Learnings Vault will be more of an online journal of my programming studies. If my vault may help some people then I can hopefully consider it as my contribution to the community😁.
I recently started learning Java and even after going through the concepts I have written in my physical notebook, I found shortcomings in my knowledge while I was attending college lectures of the same. So, this blog post will be about whatever I learn during my journey in Java where I can learn from others' critics and suggestions.

Suggestions and resources to improve me are greatly appreciated.💖 So, without any further blabbering, let's begin.

## What is Java?
Java is a general-purpose, class-based, object-oriented programming language designed for having lesser implementation dependencies. It is fast, secure, and reliable.

## How Java execution differs from other Object-Oriented Languages
In an Object-oriented language like C++, the high-level code, which is human readable, is compiled directly to a Machine Level code (0's and 1's). This machine code is platform-dependent (ie., depends on the OS it is compiled on). For example, if a C++ file is compiled on a Windows machine, then the compiled machine code (.exe file) will not run on a Mac machine and vice versa.

![cpp working.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1630729148646/J4f0hcDua.png)

To overcome this issue, Java produces Byte code (.class file) on the compilation, which is platform-independent which can be shared among machines having different Operating Systems. But this byte code cannot be directly run on a system and requires a Java Virtual Machine (JVM) to be executed. This JVM is platform-dependent ie., each OS has platform-specific versions of JVM that are used to execute this byte code.

![java working.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1630730456793/tNkDBEcS6.png)

## The Java Structure

![java structure.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1630731088028/EBP0L14AI.png)
WOW! That looks layered and complex😱 Trust me, IT IS🙂

So, let's begin with the outermost layer.

### Java Development Kit (JDK)
The JDK provides an environment to develop and execute (run) Java programs. It is a package which includes:
1. Development Tools - Environment to develop programs
2. Java Runtime Environment (JRE) - To execute the programs
3. A compiler (javac)
4. Archiver (jar)
5. Documentation Generator (javadoc)
6. Interpreter/Loader

### Java Runtime Environment (JRE)
The JRE (also written as Java RTE) is an installation package that provides the minimum requirements to ONLY execute a Java program onto your machine. It consists of:
1. Deployment technologies
2. User Interface Toolkits
3. Integration Libraries
4. Base Libraries
5. Java Virtual Machine (JVM) - JRE provides all the libraries & files the JVM needs


### Java Virtual Machine (JVM)
JVM is an abstract machine/specification that provides the runtime environment in which the java bytecode can be executed. The JVM performs the following operations:
1. Loads code
2. Verifies code
3. Executes code
4. Provides runtime environment

#### JVM Architecture

![JVM Architecture.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1630818507258/5o9HX42jo.png)
On compilation by the java compiler (javac), the Java source file (.java file) gives a Byte code file (.class file) which is then given as an input to the JVM, specifically the Class Loader Subsystem.

![Class Loader Subsystem.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1630819518948/CYNFK6U9u.png)
The Class Loader Subsystem mainly has 3 functions: 
1. **Loading**: It reads the .class file and generates binary data. An object of this class is created in heap. This further contains 3 modules - Bootstrap Class Loader, Extension Class Loader, and Application Class Loader.
2. **Linking**: The main functions of Linking include verification, preparing, and resolving. Here, the JVM verifies the .class file, allocates memory for class variables, and the default values, and replaces symbolic references from the type with direct references.
3. **Initialization**: All the static variables are assigned with their values defined in the code and the static block.
In JVM execution, the interpreter executes the code line by line. But one, limitation is that when a method is called many times, it will interpret it again and again.


> The execution inside the Class loader subsystem is Loading -> Linking -> Initialization.


The JVM also contains stack and heap memory allocations but I won't be going into too many details to avoid information overload and confusion (neither do I understand the details😣). It is fine if you don't understand the above architecture completely, neither do I. The basic gist of it should be enough for the beginning, we'll get back to the details later.

> An easier way to understand the relation between the JVM and JRE is:
The JRE is a box and JVM is the content inside the box which does the main work.

### Just-In-Time (JIT)
It is also called the garbage collector and its main purpose is to solve the limitation of JVM execution. The methods that are repeatedly called, JIT provide direct machine code so the re-interpretation is not required which results in faster execution.

### Summary of the JVM Working Architecture
To summarize the working architecture of Java in a simpler diagram for better understanding you can look at the below diagram.

![Summary of working architecture.PNG](https://cdn.hashnode.com/res/hashnode/image/upload/v1630821171404/Q7TX-ITYI.png)

I guess this should be enough theory for beginners like me, even if isn't completely understood it's fine. We'll continue next time starting with the coding part (hopefully👀).
Any suggestions or resources to increase my understanding and which would be helpful to others are greatly appreciated.💖

PS: You can also find me on [HashNode](https://hashnode.com/@Rakurai)

Until next time. Bye👋