#4 How Java Works
Understanding Java Execution: From Code to JVM
The Problem with Running Java Code
- The initial attempt to run the code fails, highlighting a need for understanding how Java applications operate behind the scenes.
- The importance of the Java Virtual Machine (JVM) is introduced, emphasizing that it allows Java's platform independence across various operating systems like Windows, Linux, and Mac.
JVM and Its Environment
- The relationship between the OS and hardware is explained; JVM operates on top of an OS which in turn runs on hardware.
- JVM's role is clarified as executing Java code, but it does not accept raw code directly from programmers.
Platform Independence Explained
- Platform independence means that a Java application can run on any machine with a JVM installed, regardless of hardware or OS.
- While applications are platform-independent, the JVM itself is platform-dependent; it must be built for specific operating systems.
Bytecode and Compilation Process
- To execute code, developers must convert their readable Java code into bytecode—a format understandable by the JVM—using a compiler known as
javac.
- This process involves writing human-readable code which then gets compiled into bytecode before being executed by the JVM.
Project Structure and Main Method Requirement
- In larger projects with multiple files, only one file will be executed first. Developers must specify this entry point when running their application.
- Each main file must contain a
mainmethod with a specific signature (public static void main(String[] args)), which serves as the starting point for execution.
Key Steps in Running a Java Application
- The steps to follow include creating a Java file, compiling it into bytecode using
javac, and finally running that bytecode on the JVM.
Understanding Java Code Structure and Execution
Introduction to JVM and Code Execution
- The Java Virtual Machine (JVM) initiates code execution by looking for specific syntax in the code, which is essential for generating output.
- It is crucial to encapsulate your code within a
mainmethod, as this is what the JVM requires to execute the program.
Main Method Syntax
- The syntax for defining the main method in Java is
public static void main(String[] args), which will be elaborated on in future sessions.
- In object-oriented programming, everything revolves around objects; thus, a class must be defined to create these objects.
Class Definition and Indentation
- A class definition must accompany your code; it should have the same name as your file. Proper indentation using tabs enhances readability.
- Avoid unnecessary suggestions from development tools during teaching; focus on writing clean and understandable code.
Compiling and Running Java Code
- After compiling your Java file, a bytecode file with a
.classextension is generated automatically. This bytecode is essential for running the application.
- To run the compiled code, use the command
java Hello, where "Hello" refers to the class name—not the filename—resulting in output like "Hello World".
Concept of JRE and JVM
- Running a Java application requires additional libraries and runtime environments similar to needing ingredients when cooking; these are stored within an environment called JRE (Java Runtime Environment).
- The JVM (Java Virtual Machine), part of JRE, executes Java applications. Understanding both components is vital for effective coding.
Developer Tools: JDK vs. JRE
- The JDK (Java Development Kit), used by developers, includes both JRE and JVM. However, only JRE needs to be present on machines running compiled applications.
- When distributing applications, ensure that only JRE (and not JDK) is required on other machines for successful execution.