Compare JDK, JRE and JVM #
Java has key components like JDK, JRE, and JVM, each serving different purposes.
Three Common Tasks with Java Programs:
- Compile Programs: Convert Java source code into class files or jar files.
- Run Programs: Execute the compiled Java class files or jar files.
- Debug Programs: Identify issues like performance problems, errors, and memory usage.
Types of Java Consumers:
- Developers: Compile, run, and debug Java programs. They require the JDK (Java Development Kit).
- Users: Run pre-compiled Java programs, such as games or applications. They only need the JRE (Java Runtime Environment).
Java Virtual Machine:
- Converts bytecode into machine-executable code that can be executed
Java Runtime Environment (JRE):
- Runs compiled Java code
- Consists of the JVM (Java Virtual Machine) and Java libraries.
Java Development Kit (JDK):
- Used for developing Java programs.
- Includes the JRE and developer tools like:
javac: Compiles Java code into class files.jar: Creates jar files.- Debugging tools: Analyze performance and troubleshoot issues.
Hierarchy of Components:
- JVM: Runs Java bytecode.
- JRE: JVM + Libraries → For running programs.
- JDK: JRE + Development tools → For developing programs.
- Each one is a superset of the other: JDK ⊃ JRE ⊃ JVM.
Summary:
- To Run Programs: Use JRE.
- To Develop Programs: Use JDK.
Differences between Java and C++ #
Platform Independence:
- Java: Platform independent; write once, run anywhere. Compiled Java bytecode can run on any operating system using the JVM.
- C++: Not platform independent; compiled code generates object code specific to the operating system.
Object-Oriented Purity:
- Java: More object-oriented than C++ but not fully pure. Java includes primitives that are not objects.
- C++: Allows structural programming, meaning you can write C-style programs without using objects.
Pointers:
- C++: Supports pointers, which allow direct memory access. This can pose security risks.
- Java: Does not support pointers, enhancing security.
Memory Management:
- C++: Programmers are responsible for manual memory allocation and deallocation.
- Java: Automatic memory management using garbage collection. Objects are cleaned up when they go out of scope.
Multiple Inheritance:
- C++: Supports multiple inheritance, allowing a class to inherit from more than one class.
- Java: Does not support multiple inheritance to avoid ambiguity; instead, it uses interfaces for similar functionality.
Summary:
- Java emphasizes platform independence, security, and simplicity with automatic memory management.
- C++ provides greater control over memory and supports multiple inheritance but lacks platform independence and has additional complexity like pointers.
Explain Different Class Loaders in Java #
Role of a Class Loader: Dynamically loads classes into memory during Java program execution.
Types of Classes in Java:
- Programmer-Defined Classes: Written by developers.
- Framework Classes: Provided by libraries like Spring or Struts.
- System Classes: Core Java classes (e.g., collections,
java.lang).
Types of Class Loaders:
- Bootstrap Class Loader:
- Loads core Java libraries (
java.lang.*) from the JRE.
- Loads core Java libraries (
- Extension Class Loader:
- Loads classes from the extensions directory (
lib/ext).
- Loads classes from the extensions directory (
- System Class Loader (Application Class Loader):
- Loads classes from the classpath (specified folders and jars).
Class Loader Hierarchy:
- Bootstrap Class Loader → Extension Class Loader → System Class Loader.
- Each class loader delegates the search to its parent first (parent delegation model).
Summary:
- Bootstrap: Loads core Java libraries.
- Extension: Loads libraries from
lib/ext. - System: Loads application classes from the classpath.
Why Is Java So Popular? #
Platform Independence:
- Java programs are "write once, run anywhere".
- Once compiled into a JAR file, the same program can run on any operating system—Windows, Mac, or Unix.
Object-Oriented Programming:
- Java’s object-oriented nature results in maintainable and scalable code, which is ideal for large enterprise applications.
What Is Platform Independence? #
- Platform independence means that a program can be built on one operating system and executed on any other operating system without modification.
- This is summarized as "Build Once, Run Anywhere".
How Does Java Achieve Platform Independence? #
Java Code and Bytecode:
- Java source code is written in
.javafiles. - When compiled, the code produces
.classfiles, which contain an intermediate representation called bytecode.
Role of the JVM (Java Virtual Machine):
- The JVM converts bytecode into executable instructions specific to the underlying operating system.
- Each operating system has its own JVM:
- A Windows JVM generates instructions for Windows.
- A Unix JVM generates instructions for Unix.
- All JVMs share one common feature: they understand bytecode and translate it into the instructions of their specific operating system.
Summary:
- Bytecode: An intermediate representation produced by the Java compiler.
- JVM: Converts bytecode to executable instructions specific to the operating system.
- JVMs are different for each OS, but they all understand and process the same bytecode, enabling Java’s platform independence.
Java achieves platform independence by using the combination of bytecode and the Java Virtual Machine, ensuring compatibility across different operating systems.