Java's Architecture

A dive into the layout of the world's 7th most used language.

Java's Architecture

Hello everyone, this is Shikhar. I hope you are all doing well. Today, I have a question for you: how do you learn a new software or programming language? There are various ways to learn, such as using the software, reading its documentation, or taking a course. Each method has its advantages and disadvantages. However, it is important to note that every software has an architecture on which it is built. Therefore, understanding the software's architecture can help learn it.

Today I will be discussing Java’s architecture. We will talk about the journey from Java source code to 🤏 tiny little binary bits. Whether you're an experienced developer or just starting, understanding the underlying structure of this powerful language will deepen your knowledge and expand your skill set.

Oracle Java offers two Java platforms: Java SE(Standard Edition) and Java EE(Enterprise Edition). This blog's scope will be limited to the Java SE platform.

The diagram above illustrates the components of Oracle's Java Platform Products. Note: The diagram describes Java SE 8. Contents could change.

Java SE

Java SE is the most popular modern development platform. It provides the core functionality of the Java programming language and includes the Java Development Kit (JDK) which includes tools for developing Java applications, as well as the Java Runtime Environment (JRE) which allows users to run Java applications.

Java SE is used for developing desktop, mobile and enterprise-level applications. Thanks to JVM these applications follow the “write once, run anywhere" principle, meaning that Java applications can run on any platform that has a compatible Java Virtual Machine (JVM) installed, without needing to be recompiled for each platform.

Java SE provides a rich set of APIs (Application Programming Interfaces) for tasks such as input/output, networking, database access, graphical user interfaces (GUI), and more. It also includes features such as multithreading, exception handling, and security. Knowing that we can say Java SE is a feature-rich platform.

As we can see, there are two principal products in the Java SE family: Java SE Runtime Environment (JRE) and Java Development Kit (JDK). Let me define them for you.

Java Runtime Environment

The Java Runtime Environment (JRE) provides the libraries, the Java Virtual Machine, and other components to run applets and applications written in the Java programming language. The primary purpose of JRE is to provide an execution environment for Java applications.

The primary purpose of JRE is to provide an execution environment for Java applications. JRE also contains a class loader subsystem which is responsible for loading Java classes in memory as they are required during the program execution. It ensures that they are properly initialized and linked.

In addition, two key deployment technologies are part of the JRE: Java Plug-in, which enables applets to run in popular browsers; and Java Web Start, which deploys standalone applications over a network. This runtime environment is generally redistributed with applications to make them free-standing. JRE also boasts base libraries(lang and util), integration libraries(JDBC, RMI etc) and UI tools (AWT, Java 2D etc). We will see them in detail later in this blog.

Java Development Kit

The JDK is a superset of the JRE and contains everything that is in the JRE, plus tools such as the compilers and debuggers necessary for developing applets and applications.

Extras on top of JRE :

  • Java Compiler (javac):

    Javac translates Java source code into bytecode, a platform-independent intermediate representation of the program. This bytecode can then be executed by the Java Virtual Machine (JVM) on any platform that supports Java.

  • Java Development Tools:

    These tools aid in the creation and maintenance of Java applications.

    • Java Debugger (jdb): A command-line debugger for troubleshooting and fixing Java programs.

    • Java Archive (JAR) Tool: A utility for creating, managing, and extracting JAR files, which are packages containing compiled Java classes and resources.

    • JavaDoc Tool: A documentation generator that produces HTML documentation from Java source code comments.

    • JavaFX Packager: Tools for packaging JavaFX applications for distribution.

    • JavaFX Compiler: A tool for compiling JavaFX applications.

    • Java Mission Control (JMC): A tool for monitoring, profiling, and troubleshooting Java applications.

    • Java Flight Recorder (JFR): A profiling tool for collecting diagnostic and performance data from Java applications.

  • JavaFX:

    JavaFX is a platform-independent GUI toolkit for creating rich internet applications, desktop applications, and mobile applications.

All around, the Java Development Kit (JDK) is a comprehensive software development kit that provides developers with all the tools and libraries necessary to build robust, scalable, and cross-platform Java applications.

Java Virtual Machine

The Java Virtual Machine (JVM) is a crucial component of the Java Runtime Environment (JRE) and acts as the runtime engine for Java applications.

The JVM interprets Java bytecode, the compiled form of Java source code, and executes it on the host system. It translates bytecode instructions into machine-specific instructions, allowing Java applications to run on any platform with a compatible JVM.

Modern JVM’s use Just-In-Time compilation to improve performance. The JIT compiler dynamically compiles bytecode into native machine code at runtime, optimizing performance by identifying and optimizing frequently executed code paths. JVM uses a class loader for dynamically loading classes during execution.

For security, JVM exhibits a security manager that enforces security policies to protect the host system from potentially malicious code.

The JVM manages memory allocation and deallocation for Java applications. It includes a garbage collector that automatically deallocates memory used by objects no longer referenced, preventing memory leaks and ensuring efficient usage.

Explained above 🙄 is a very brief explanation of JVM. A comprehensive explanation of JVM might as well require a whole new blog.

Java SE API

The Java SE application programming interface (API) defines how an applet or application can request and use the functionality available in the compiled Java SE class libraries. (The Java SE class libraries are also part of the Java SE platform.)

The API documentation provided by Oracle includes detailed information about each class, interface, method, and field, along with examples and usage guidelines.

Open JDK

OpenJDK is a free and open-source implementation of Java SE, which is maintained by the OpenJDK community comprising experts from worldwide individuals and organizations.

OpenJDK aims to be compatible with the Java SE specification to ensure that applications developed on it can run on other Java SE implementations and vice versa.

OpenJDK is licensed under the GNU General Public License (GPL) version 2, allowing free use, modification, and distribution, commercially and non-commercially. Some OpenJDK components may be licensed under other open-source licenses.

OpenJDK is widely adopted in various environments, including development, testing, and production. Many Linux distributions include OpenJDK as their default Java runtime environment (JRE) and development kit (JDK), making it readily available to users.

OpenJDK is a vital component of the Java ecosystem. It provides developers with a dependable and freely available implementation of the Java platform, giving them confidence to create Java applications.

Example :

In this example, we will first compile a Java file. Then we will execute the resulting file. Generally, while using IDE's clicking the run button performs these two steps for us.

  • Compilation :

    The following code adds the adder variable to the sum variable and prints it to the console.

      import java.lang.System;
    
      public class Main {
          public static void main(String[] args) {
              int num = 7; 
              int adder = 10; 
              int sum = num + adder; 
              System.out.println(sum);
          }
      }
    
  • javac file.java

    Using the Javac compiler this command will create a file.class file in the same directory. That file contains the java bytecode for the file.java file. Now we just need to execute that file, so that JVM can do its magic.

    • This is how a .class file looks like :

      Easy to understand, right??? 😆😆

  • Execute .class file :

    java -cp <classpath> file.java

    -cp uses classpath to find .class file.

In conclusion, understanding the architecture of any programming language or software can help you learn it better. Java's architecture is robust and feature-rich, making it a popular choice for developing desktop, mobile, and enterprise-level applications. By having a deeper understanding of Java's architecture, developers can take full advantage of its capabilities and develop high-quality software applications.