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.
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.
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.
Java is a general-purpose, class-based, object-oriented programming language designed for having lesser implementation dependencies. It is fast, secure, and reliable.
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.
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.
So, let's begin with the outermost layer.
The JDK provides an environment to develop and execute (run) Java programs. It is a package which includes:
- Development Tools - Environment to develop programs
- Java Runtime Environment (JRE) - To execute the programs
- A compiler (javac)
- Archiver (jar)
- Documentation Generator (javadoc)
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:
- Deployment technologies
- User Interface Toolkits
- Integration Libraries
- Base Libraries
- Java Virtual Machine (JVM) - JRE provides all the libraries & files the JVM needs
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:
- Loads code
- Verifies code
- Executes code
- Provides runtime environment
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.
- 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.
- 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.
- 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.
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.
To summarize the working architecture of Java in a simpler diagram for better understanding you can look at the below diagram.
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
Until next time. Bye👋