Introduction to Java
What Java is, why it is popular, how the JVM works, and how to run your first Java program.
Learning objectives
- βUnderstand what Java is and where it is used
- βLearn the role of the JVM, JRE and JDK
- βCompile and run a Hello World program
π‘ Key points
- Java is a statically-typed, object-oriented, class-based language.
- Java code is compiled to bytecode (.class) and executed by the JVM β 'write once, run anywhere'.
- JDK = compiler + tools + JRE. JRE = JVM + libraries.
- Every Java program starts from a public static void main(String[] args) method.
π» Code examples(2)
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Hello, World!
javac Hello.java # produces Hello.class
java Hello # runs the bytecode
π― Practice
Q1. What does JVM stand for and what is its job?+
Java Virtual Machine β it loads and executes Java bytecode on any platform that has a compatible JVM installed.
Q2. Why must the class containing main be public and the method static?+
public so the JVM can access it from outside the class; static so the JVM can call it without creating an instance first.
π Notes
Why learn Java?
Java runs on billions of devices β Android apps, enterprise backends, Big Data (Hadoop, Spark), and embedded systems. Learning Java teaches you object-oriented thinking that transfers to C#, Kotlin, and Scala.
The Java toolchain
| Component | What it is |
|---|---|
| JDK | Developer kit β includes javac, java, jar, and the JRE |
| JRE | Runtime environment β JVM + standard libraries |
| JVM | The virtual machine that executes .class bytecode |
Install the JDK, verify with java -version and javac -version.