2. How Java Program Works and its 3 Important Components (JVM, JRE and JDK) with Example
By Concept && Coding - by Shrayansh
Key Concepts
Java, Platform Independence (WORA - Write Once Run Anywhere), Object-Oriented Programming (OOP), JVM (Java Virtual Machine), JRE (Java Runtime Environment), JDK (Java Development Kit), Bytecode (.class), Compiler (javac), JIT (Just-In-Time) Compiler, Class Libraries, Java SE (Standard Edition), Java EE (Enterprise Edition), Java ME (Micro Edition), Class Structure (Variables, Methods, Constructor, Nested Class), Main Method (public static void main(String[] args)).
Java Overview: Fundamentals
The video provides a fundamental overview of Java, emphasizing its platform independence and object-oriented nature. Java is defined as a platform-independent language and a popular object-oriented programming language. A major advantage of Java is its portability, embodied in the principle "Write Once, Run Anywhere" (WORA). This means a Java program written on one platform (e.g., mobile) can run on another (e.g., laptop, desktop).
Three Main Components: JVM, JRE, JDK
The video breaks down Java into three core components:
- JVM (Java Virtual Machine):
- An abstract machine (software, not physical).
- Crucial for Java's portability.
- The compilation process involves converting Java code to bytecode (
.class
files), which the JVM then translates into machine code specific to the underlying operating system. - The JVM contains a JIT (Just-In-Time) compiler that converts bytecode to machine code.
- The JVM is platform-dependent; a specific JVM implementation is required for each operating system (Windows, macOS, Linux).
- Example: Compiling
Student.java
usingjavac Student.java
createsStudent.class
(bytecode). This.class
file can be run on any system with a compatible JVM.
- JRE (Java Runtime Environment):
- Consists of the JVM plus class libraries.
- Class libraries provide pre-built functionalities (e.g.,
Math.abs()
fromjava.lang
,Arrays.sort()
fromjava.util
). - The JVM uses these libraries to resolve dependencies during runtime.
- With only JRE, you can run Java programs (bytecode) but cannot compile or develop them.
- JRE contains JVM.
- JDK (Java Development Kit):
- Includes the JRE plus development tools like the Java compiler (
javac
), debugger, and other utilities. - Essential for writing, compiling, and debugging Java code.
- JDK = JRE + Programming Language + Compiler + Debugger.
- Downloading the JDK provides JRE and JVM.
- Includes the JRE plus development tools like the Java compiler (
Platform Dependence vs. Independence
- The Java code itself, once compiled into bytecode, is platform-independent.
- The JVM, JRE, and JDK are platform-dependent.
Downloading and Installing JDK
The video demonstrates how to download the JDK from the Oracle website. It recommends downloading directly from Oracle for authenticity. After installation, the command java -version
can be used to verify the installation and check the Java version.
Java Editions: SE, EE, ME
The video briefly explains the different Java editions:
- Java SE (Standard Edition): Core Java, used for general-purpose programming, including classes, objects, and multithreading.
- Java EE (Enterprise Edition): Java SE plus APIs for building large-scale applications, including transactional APIs (commit, rollback), servlets, Java Server Pages (JSP), and persistence APIs for database management.
- Java ME (Micro Edition) / Jakarta EE: APIs for mobile applications and resource-constrained environments.
The video notes that developers typically work with Java SE (Core Java).
Writing the First Java Program
The video guides the viewer through writing a basic Java program:
-
Class Structure: A class can contain variables, methods, constructors, and nested classes.
-
File Naming: The file name (e.g.,
Employee.java
) should match the class name (e.g.,public class Employee
). A file can only have one public class. -
Main Method: The
main
method (public static void main(String[] args)
) is the entry point of the program. The JVM calls this method to start execution.void
indicates that the method doesn't return any value.public
means the method can be accessed from anywhere, including outside the package.static
means the method belongs to the class itself, not an instance of the class (object). It can be called directly using the class name.
-
Comments:
- Single-line comments:
// This is a comment
- Multi-line comments:
/* This is a multi-line comment */
- Single-line comments:
-
Example Code:
public class Employee { public static void main(String[] args) { int a = -10; System.out.println("This is my first program and output of a is " + a); } }
-
Compilation and Execution:
- Compile:
javac Employee.java
(createsEmployee.class
- bytecode) - Run:
java Employee
(executes the bytecode using the JVM)
- Compile:
-
Important Note: Changes to the
.java
file require recompilation to update the.class
file. The JVM executes the.class
file, not the.java
file.
Conclusion
The video provides a comprehensive overview of Java's core concepts, including its platform independence, the roles of the JVM, JRE, and JDK, and the process of writing and running a basic Java program. It emphasizes the importance of understanding bytecode and the compilation process. The video also touches on different Java editions and provides practical guidance on downloading and installing the JDK. The key takeaway is that Java's portability stems from the JVM's ability to interpret bytecode, allowing Java programs to run on any platform with a compatible JVM.
Chat with this Video
AI-PoweredHi! I can answer questions about this video "2. How Java Program Works and its 3 Important Components (JVM, JRE and JDK) with Example". What would you like to know?