Welcome & Hello World
Welcome to Java! Java is a robust, class-based, object-oriented programming language used by millions of developers globally. In this first lesson, we will understand how Java works, look at its compilation model, and write our first program.
Unlike languages that compile directly to raw machine code (like C++) or are directly interpreted line-by-line (like Python), Java utilizes a unique two-stage compilation and execution system:
- JDK (Java Development Kit): The toolkit used by developers to write and compile programs. It contains the compiler (`javac`).
- JRE (Java Runtime Environment): The environment required to run Java applications. It contains the JVM and core library classes.
- JVM (Java Virtual Machine): The engine that executes compiled Java bytecode. The JVM translates bytecode into local computer instruction sets.
The Workflow: You write Java source code (`.java`), compile it using `javac` into Bytecode (`.class`), and run it inside the JVM on any system. This is what enables "Write Once, Run Anywhere" (WORA).
Let's analyze the classic "Hello, World!" program in Java. In Java, every line of executable code must exist inside a class definition:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.print("Welcome to Our Compiler!");
}
}
Let's break down the method signature word-by-word:
- public: Visible to everyone. The JVM must access this method to run your application.
- static: The JVM can call this method without instantiating an object of the class.
- void: The method returns no value.
- main: The keyword name that acts as the entry point of every Java program.
- String[] args: An array of text strings passed as arguments to the program via command line execution.
Note: System.out.println() writes the text and moves the cursor to a new line, whereas System.out.print() keeps the cursor on the same line.