Home » Java Architecture and Components

Java Architecture and Components

Java Architecture and Components

Java Architecture is a collection of components, namely JVM (Java Virtual Machine), JRE (Java Runtime Environment), and JDK (Java Development Kit). It orchestrates the process of both interpretation and compilation, delineating all processes involved in creating a Java program.

Components of Java Architecture

Java architecture comprises three main components:

  • Java Virtual Machine (JVM)
  • Java Runtime Environment (JRE)
  • Java Development Kit (JDK)

Components-of-JDK-Development.png
Components-of-JDK.png

Java Programming Terminology

Let’s try to understand the various terms frequently used in Java and how the code in Java is executed. The flow chart given below shows how a Java program is executed.

java-programming-terminology.png

Java Virtual Machine Architecture

The main feature of Java is WORA. WORA stands for Write Once Run Anywhere. The feature states that we can write our code once and use it anywhere or on any operating system. Our Java program can run any of the platforms only because of the Java Virtual Machine. It is a Java platform component that gives us an environment to execute java programs. JVM’s main task is to convert byte code into machine code.

JVM, first of all, loads the code into memory and verifies it. After that, it executes the code and provides a runtime environment. Java Virtual Machine (JVM) has its own architecture, which is given below:-

Java-Virtual-Machine-Architecture.png

The JVM performs following operation:

  • Loads code
  • Verifies code
  • Executes code
  • Provides runtime environment

The Power Of JVM: Making Java Programs Portable

1. Interpreting ByteCode:—Think of JVM as a language translator for computers. It takes Java code, known as bytecode, and translates it into instructions that different operating systems can understand. It’s like having a translator who can convert your messages into any language, allowing you to communicate with people from different backgrounds.

2. Managing Memory:—JVM is like a smart organizer that efficiently handles memory for Java programs. It ensures that memory space is allocated and released properly, just like a closet organizer keeps your belongings neat and tidy. This efficient memory management helps programs run smoothly and prevents memory-related issues.

3. Handling Errors:—Imagine driving a car with built-in safety features. Similarly, JVM acts as a safety net for Java programs. It detects errors, called exceptions, that can occur during program execution. Like the safety system in a car that prevents accidents and keeps you safe, JVM manages exceptions, allowing programs to handle errors gracefully and continue running.

4. Portability Across Platforms:—-Consider a universal adapter that allows you to charge your devices anywhere in the world. Similarly, JVM enables Java programs to run on different platforms. It acts as a universal language translator that understands Java bytecode and adapts it to work on various systems. This means you can write a Java program once and run it on any platform with a compatible JVM, just like using your devices with the right adapter wherever you go.

5. ClassLoader: The Java Class Loader is a part of the Java Runtime Environment that dynamically loads Java classes into the Java Virtual Machine.

jvm-working.png

How Does the ClassLoader Subsystem Work?

When your Java program runs, the Class Loader Subsystem follows a specific sequence of steps to load and prepare the classes:

Loading: The Class Loader Subsystem searches for the required class files based on their names. It looks in various locations, such as directories or JAR files, to find these files. It’s like a detective on a mission, tracking down the right suspects to solve the case.

Linking (Verification, Preparation, and Resolution):The Class Loader Subsystem performs three essential functions in the linking process: verification, preparation, and resolution.

Verification: This step validates the reliability and accuracy of the class files. It looks for any possible coding faults or security issues. It’s similar to conducting a background check on people before trusting them with important duties.

Preparation: The Class Loader Subsystem sets up the class by allocating memory for its variables and functions. It provides the necessary data structures for the class to be successfully executed. It resembles setting up a workspace with all the equipment and supplies required to finish a task.

Resolution: Resolution involves resolving any dependencies the class may have on other classes or libraries. It ensures that all required classes are available and linked correctly. It’s like connecting the dots and ensuring that all the pieces fit together seamlessly.

Initialization: After linking, the Class Loader Subsystem initializes the class by executing the static blocks of code and assigning initial values to static Java variables. It’s like setting up the stage before a play begins, ensuring everything is ready for a seamless performance.

ClassLoader-Subsystem-Work.png

Runtime Method Areas In JVM Architecture

Class Method Area:-The class method area is the memory block that stores the class code, variable code(static variable, runtime constant), method code, and the constructor of a Java program. (Here method means the function which is written inside the class).

Heap:The Java heap is the area of memory used to store objects instantiated by applications running on the JVM architecture. When the JVM is started, heap memory is created and any objects in the heap can be shared between threads as long as the application is running.

Stack: It is also referred to as thread stack. It is created for a single execution thread. The thread uses this area to store the elements like the partial result, local variable, data used for calling method and returns etc.

PC Register:The PC Register is like a helpful guide. It keeps track of the current instruction being executed by your program. It’s like having a GPS telling you where to go next. The PC Register makes sure your program follows the right path and keeps things running smoothly.

Native Stack: It contains the information of all the native methods used in our application.

Execution Engine: It is the central part of the JVM. Its main task is to execute the byte code and execute the Java classes. The execution engine has three main components used for executing Java classes.

JVM-architecture-methods.png
  • Interpreter: It converts the byte code into native code and executes. It sequentially executes the code. The interpreter interprets continuously and even the same method multiple times. This reduces the performance of the system, and to solve this, the JIT compiler is introduced.
  • JIT Compiler: JIT compiler is introduced to remove the drawback of the interpreter. It increases the speed of execution and improves performance.

The combination of the Interpreter and JIT Compiler in the Execution Engine provides a powerful duo that maximizes the performance of your Java program. While the Interpreter ensures the accurate execution of code, the JIT Compiler enhances efficiency by optimizing frequently used code segments. By dynamically analyzing and optimizing your code during runtime, the Execution Engine helps your program achieve better performance. It’s like having a team of experts constantly fine-tuning and improving your program’s execution.

  • Garbage Collector: The garbage collector is used to manage the memory, and it is a program written in Java. It works in two phases, i.e., Mark and Sweep. Mark is an area where the garbage collector identifies the used and unused chunks of memory. The Sweep removes the identified object from the Mark

Java Runtime Environment

The Java Runtime Environment (JRE) is software that Java programs require to run correctly. Java is a computer language that powers many current web and mobile applications. The JRE is the underlying technology that communicates between the Java program and the operating system.

JRE is like a supporting actor that ensures Java applications have everything they need to perform their best.

Components of JRE

Java Virtual Machine (JVM): JVM acts as the director, translating Java code into a language that the computer understands. Imagine JVM as a translator at the United Nations. It listens to speeches in different languages and translates them for everyone to understand.

Java Class Libraries: JRE provides a library of ready-to-use code modules that simplify application development and solve common programming challenges. Java Class Libraries are like a toolbox filled with tools. Developers can borrow these tools instead of building everything from scratch, saving time and effort.

Runtime Environment: JRE creates a comfortable environment for Java applications to perform their best, taking care of memory management, garbage collection, and security. Think of the runtime environment as a well-equipped kitchen where chefs can focus on cooking delicious meals without worrying about ingredient availability or kitchen cleanliness.

component-of-java.png

Java Development Kit

JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a software development environment which is used to develop java applications and applets. It physically exists. It contains JRE + development tools.

java-development-Kit.png

The JDK contains a private Java Virtual Machine (JVM) and a few other resources such as an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (Javadoc) etc. to complete the development of a Java Application.

Java Development Tools

The JDK includes various tools that help in the development process

Java Compiler

The Java compiler is like a language translator that converts your human-readable Java code into a machine-readable format. It checks the syntax and detects errors. It’s similar to a spell-checker who reviews your writing, identifies mistakes, and ensures the text is accurate and understandable.

Javadoc: It generates documentation for your Java code, similar to a technical writer who creates user manuals or API documentation for a software product.

Jar (Java Archive): This tool helps you package your Java classes and resources into a single archive file, like a zip file, for distribution or deployment. It’s similar to packaging a product in a box before shipping it to customers.

JDB (Java Debugger): JDB allows you to debug your Java programs, similar to a detective who investigates and solves mysteries. It helps you find and fix issues by stepping through the code, setting breakpoints, and inspecting variables.

By leveraging the components of the JDK effectively, you can write robust and reliable Java applications that run seamlessly across different platforms, just as actors deliver captivating performances on a well-equipped stage, ensuring a memorable experience for the audience.

JDK’s Significance In Java Development

A key component of Java programming is the JDK. It provides the tools and libraries developers need to develop, put together, and run Java applications. The JVM, which is in charge of offering the runtime environment for Java programs, is also part of the JDK.

What Does Platform Independence Mean?

A platform is a software or hardware on which a program operates regularly. A platform for this language, the Java Runtime Environment, is available. Platform independence refers to the program’s independence from the system’s operating system. It adopts the “Write Once Run Anywhere (WORA)” methodology.

java-platform-independent-atrchitecture.png

How is Java platform independent?

The meaning of Java platform-independent is that the Java compiled code(byte code) can run on all operating systems.in java the bytecode produced by the javac compiler may be run on a variety of Operating Systems.

In Java, the compiler converts the program into bytecode, which is not executable code. Now, any other platform or operating system may run this bytecode. When we execute its program or file, we obtain a .class file, which is a non-executable file. We would require a virtual machine to execute a .class file. Java JVM is undoubtedly platform-independent architecture. Similar to how it varies depending on the surroundings. For instance, MAC and Linux have a different JVM than Windows. It supports portability as one of its key features since its code’s portable bytecode makes it platform-independent.

java -architecture.png

Difference Between JDK, JRE and JVM

JDKJREJVM
The full form of JDK is Java Development KitThe full form of JRE is Java Runtime Environment.
JVM
The full form of JVM is Java Virtual Machine.
JDK is a software development kit to develop applications in Java.it is a software bundle which provides Java class libraries with necessary components to run Java code.
JVM executes Java byte code and provides an environment for executing it.
JDR is platform dependent.
JRE is also platform dependent.JVM is platform- independent.
It contain tools for developing debugging and monitoring java code.It contains class libraries and other supporting files that JVM requires to execute the program.Software development tools are not included in JVM.
It is the superset of JREIt is the subset of JDK.JVM is a subset of JRE
The JDK enables developers to create Java programs that can be executed and run by the JRE and JVM.The JRE is the part of Java that creates the JVM.It is the Java platform component that executes source code.
JDK comes with the installerJRE only contain environment to execute source code.JVM bundled in both software JDK and JRE

Conclusion

Java architecture stands as a testament to the transformative power of a well-designed programming language and runtime environment. From its inception to the present day, Java has remained at the forefront of software innovation, enabling developers to overcome technological challenges and deliver impactful solutions across industries. As we look towards the future, the principles of Java architecture will continue to guide the development of robust, scalable, and resilient software, ensuring its relevance in the ever-changing landscape of technology.

Frequently Asked Questions

Q1. Can Java run on any platform without the need for recompilation?

Ans: Yes, Java bytecode can run on any platform that has a compatible JVM, without the need for recompilation.


Q2. What is the role of the Garbage Collector in JVM?

Ans: The Garbage Collector manages memory by automatically freeing unused objects, ensuring efficient memory utilization.


Q3. How does the JIT Compiler improve Java program performance?

Ans: The JIT Compiler improves performance by converting frequently executed bytecode into optimized machine code, reducing execution time.


Q4. Can I develop Java applications with just the JRE installed?

Ans: No, for developing Java applications, you need the JDK, which includes the JRE and development tools. JRE alone is sufficient for running Java applications.


Q5. What makes Java platform-independent?

Ans: Java’s platform independence comes from its use of bytecode, which can be executed by the JVM on any platform, eliminating the need for platform-specific code.