Just got up, brushed my teeth and showered...to boot up my computer to write about my true love, Java! BTW, it is day 8 in my #180DaysOfDSA challenge. So, today I decided to revisit my true love Java - whom I met back in 2019. I am spending a lot of time with Javascript right now. I haven't broken up with Java but we don't talk anymore🥺
Anyway, Java is beautiful. I met her a few years back and fell in love with her. She was a little bit complex to understand but I got the hang of her after spending a substantial amount of time with her. I had a romantic time with her during lockdown back in 2020 that is still fresh in my memories. I still live in those memories😋. Allow me to describe her beauty...
She is a high-level, object-oriented programming and platform-independent language. I wish I could meet her dad- James Gosling and tell him how lovely his daughter is😁. She was born at Sun Microsystems but she lives in Oracle right now.
Here is the list of qualities she possesses that I admire.
Object-oriented: She uses classes and objects to structure and organize the code
Platform-independent: We can write code once and run it everywhere.
Multithreaded:
Interpreted:
How does Java work?
Remember the jargon JDK, JRE and JVM.
What is JDK?
Java Development Kit is a software development kit that consists of tools and components that Java provides us to craft and execute Java programs. It is packed with the following items:-
Java Runtime Environment
Java Compiler
Standard Java Class Library
What is JRE?
Java Runtime Environment is a module inside JDK that consists of Java Virtual Machine, a subset of standard Java Class Library and runtime files.
What is JVM?
Java Virtual Machine is a critical component of JDK and JRE that helps in the platform-independent execution of Java programs. It consists of an interpreter and a JIT compiler.
Finally, we can now talk about how Java works...😉
Java source code execution involves several steps, from writing the code to running the program. Here's a step-by-step explanation of how Java source code is executed:
Writing Java Source Code:
- The process begins with the development of Java source code using a text editor or an Integrated Development Environment (IDE). Developers write Java code in
.java
files, and this code defines the program's logic and behavior.
- The process begins with the development of Java source code using a text editor or an Integrated Development Environment (IDE). Developers write Java code in
Compilation (javac):
The next step is to compile the Java source code using the Java compiler, which is invoked by the
javac
command. The Java compiler reads the.java
files, performs syntax checking, and generates bytecode in the form of.class
files. These.class
files contain the compiled code that can be executed on the Java Virtual Machine (JVM).The compilation also includes resolving dependencies, such as importing other classes and libraries and generating necessary data structures for runtime execution.
Class Loading:
When a Java program is executed, the JVM loads the necessary class files into memory. This is done dynamically, and the class loading process is divided into three stages:
Loading: The class loader loads the bytecode of the class into memory.
Linking: This involves verifying and preparing the class for execution. Verification checks ensure the bytecode is valid and follows security constraints. Preparation involves allocating memory for class variables and initializing them.
Initialization: The static initializers and static blocks in the class are executed.
Bytecode Verification:
- Before executing the bytecode, the JVM performs bytecode verification to ensure that it doesn't violate any security constraints or rules. This step is crucial for maintaining the security and stability of the JVM.
Execution (JVM):
Once the bytecode is loaded and verified, it's executed by the Java Virtual Machine (JVM). The JVM interprets the bytecode line by line or may use Just-In-Time (JIT) compilation to convert the bytecode into native machine code for improved performance.
During execution, the JVM manages memory, thread execution, and exception handling. It also interacts with the native operating system to perform I/O operations and other system-related tasks.
Runtime Execution:
- The Java program runs as a series of instructions executed by the JVM. This includes calling methods, creating objects, and interacting with the standard input and output streams.
Garbage Collection:
- The JVM includes a garbage collector that periodically identifies and reclaims memory occupied by objects that are no longer reachable or referenced by the program. This helps manage memory and prevent memory leaks.
Termination:
- The Java program continues to execute until it reaches its end or until an unhandled exception occurs. At this point, the program terminates, and any necessary cleanup or resource release is performed.
Output and Results:
During execution, the program may produce output to the standard output stream (usually the console). This output can include text, error messages, or any other relevant information.
If the program is designed to produce specific results, those results are typically returned or displayed as part of the program's execution.
Program Exit:
- After completing its execution, the Java program exits. The JVM releases the associated resources, and the program's execution is complete.
It's important to note that Java's platform independence is largely achieved through the use of bytecode and the JVM. Java bytecode is a portable representation of the program that can be executed on any platform with a compatible JVM. This enables "write once, run anywhere" capability for Java applications.