JDK, JVM and JRE in Java

 



Java is a powerful, versatile programming language used to build a wide range of applications, from mobile apps to large-scale enterprise systems. A key part of working with Java effectively is understanding its core components: the JDK (Java Development Kit), the JVM (Java Virtual Machine), and the JRE (Java Runtime Environment).



Java Virtual Machine(JVM):-






The JVM is a virtual machine that enables a computer to run Java programs. It converts Java bytecode, produced by the Java compiler, into machine code that the host operating system can execute. The JVM's ability to run bytecode on any platform that has a compatible JVM implementation makes Java a "write once, run anywhere" language.

JVM Architecture:-



  1. Class Loader

    • Role: The class loader is responsible for dynamically loading Java classes into the JVM. It loads classes from the filesystem, network, or other sources as needed.
    • Types:
      • Bootstrap Class Loader: Loads core Java libraries located in the <JAVA_HOME>/lib directory.
      • Extension Class Loader: Loads classes from the <JAVA_HOME>/lib/ext directory.
      • Application Class Loader: Loads classes from the application's classpath, typically specified by the CLASSPATH environment variable or command-line options.
  2. Runtime Data Areas

    • Method Area: Stores class-level data, including field and method information, constant pool, method bytecode, and static variables.
    • Heap: The runtime data area from which memory for all class instances and arrays is allocated. The heap is managed by the garbage collector.
    • Stack: Stores method call frames, local variables, and intermediate results. Each thread has its own stack.
    • PC Registers: Each thread has its own program counter (PC) register, which keeps track of the JVM instruction currently being executed.
    • Native Method Stack: Used for native method calls, which are implemented in languages other than Java, typically C or C++.
  3. Execution Engine

    • Interpreter: Executes bytecode instructions directly. It's simple but relatively slow.
    • Just-In-Time (JIT) Compiler: Translates bytecode into native machine code at runtime. This approach significantly improves performance compared to interpretation alone.
    • Garbage Collector: Automatically manages memory by reclaiming memory occupied by objects that are no longer in use, preventing memory leaks and optimizing memory usage.
  4. Native Interface

    • Java Native Interface (JNI): Provides a way for Java code to interact with native applications and libraries written in other languages like C or C++.

The JVM is a powerful and sophisticated virtual machine that plays a critical role in the Java ecosystem. Its ability to provide platform independence, manage memory efficiently, and optimize performance through techniques like JIT compilation and garbage collection make it an essential component for running Java applications. Understanding the inner workings of the JVM can help developers write more efficient, secure, and high-performing Java applications.


Java Development Kit(JDK):-



The JDK is a software development environment used for developing Java applications and applets. It includes the JRE (Java Runtime Environment), an interpreter/loader (Java), a compiler (javac), an archiver (jar), a documentation generator (javadoc), and other tools needed for Java development.

  • Java Compiler (javac)

    • Role: The Java compiler converts Java source code (.java files) into bytecode (.class files) that can be executed by the JVM.
    • Usage: You can compile a Java file using the command javac MyClass.java, which generates a MyClass.class file.
  • Java Runtime Environment (JRE)

    • Role: The JRE provides the libraries, Java Virtual Machine (JVM), and other components needed to run Java applications. It's a subset of the JDK, tailored for end-users who want to run Java applications.
    • Components: Includes the JVM, core libraries, and other runtime components.
  • Development Tools

    • javap: A disassembler tool used to view the bytecode of class files.
    • javadoc: A tool for generating API documentation from Java source code comments.
    • jar: A tool for creating, viewing, and extracting files from Java Archive (JAR) files.
    • jdb: A command-line debugger for Java classes.
  • Libraries and APIs

    • Core Libraries: Provide essential functionality, such as data structures (java.util), networking (java.net), file I/O (java.io), and concurrency (java.util.concurrent).
    • Additional APIs: Include libraries for XML parsing, database connectivity (JDBC), and more.
  • Java Mission Control

    • Role: A suite of tools for monitoring, managing, and profiling Java applications.
    • Components: Includes JDK Flight Recorder, a tool for collecting diagnostic and profiling data about running Java applications.


Most Popular JDKs:

  • Oracle JDK: the most popular JDK and the main distributor of Java11,
  • OpenJDK: Ready for use: JDK 15, JDK 14, and JMC,
  • Azul Systems Zing: efficient and low latency JDK for Linux os,
  • Azul Systems: based Zulu brand for Linux, Windows, Mac OS X,
  • IBM J9 JDK: for AIX, Linux, Windows, and many other OS,
  • Amazon Corretto: the newest option with the no-cost build of OpenJDK and long-term support.
The JDK is a comprehensive toolkit that provides everything you need to develop, compile, debug, and run Java applications. Understanding its components and how to set it up on different platforms is essential for any Java developer. With its robust libraries, security features, and cross-platform capabilities, the JDK remains a critical tool in the Java ecosystem.


Java Runtime Environment(JRE):-




The JRE is a part of the Java Development Kit (JDK) and serves as the runtime environment for Java applications. It includes the JVM, core libraries, and other components necessary to run Java programs. Unlike the JDK, which is designed for development, the JRE is designed solely for running Java applications. It does not include development tools such as compilers and debuggers.

The Java Runtime Environment (JRE) is a comprehensive suite that includes the JVM, core class libraries, extension libraries, deployment technologies, and tools necessary for running Java applications. Each component plays a vital role in ensuring the smooth execution, performance, and security of Java applications across various platforms.

Understanding the components of the JRE provides a solid foundation for appreciating how Java applications run and interact with the underlying system. Whether you're a developer or an end-user, knowing these components helps in better managing and troubleshooting Java applications.






Comments

Popular posts from this blog

අද අපි කතා කරමු Data collections ගැන

Understanding and Mastering Coding Errors

OOP key points