Getting Started with Java
JDK, JRE, and JVM
Before writing your first Java program, it's important to understand the three core components of the Java platform.
| Component | Full Name | Description |
|---|---|---|
| JVM | Java Virtual Machine | Executes Java bytecode. Platform-specific — there is a different JVM for Windows, Linux, and macOS. |
| JRE | Java Runtime Environment | JVM + standard libraries. Required to run Java programs. |
| JDK | Java Development Kit | JRE + compiler (javac) + tools. Required to develop Java programs. |
Installing the JDK
Follow these steps to install the JDK on your machine:
Your First Java Program
Every Java program starts with a class. The entry point is the main method. Let's break down the classic Hello World program line by line.
// HelloWorld.java
public class HelloWorld { // 1. Class declaration
public static void main(String[] args) { // 2. Entry point
System.out.println("Hello, World!"); // 3. Print to console
}
}
Compile and Run
Java is a compiled language. You first compile the source file to bytecode, then run the bytecode on the JVM.
| Step | Command | Result |
|---|---|---|
| Compile | javac HelloWorld.java | Creates HelloWorld.class (bytecode) |
| Run | java HelloWorld | Executes the bytecode on the JVM |
Reading User Input with Scanner
The java.util.Scanner class lets you read input from the keyboard (standard input).
import java.util.Scanner;
public class UserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter your age: ");
int age = scanner.nextInt();
System.out.println("Hello, " + name + "! You are " + age + " years old.");
scanner.close(); // always close the scanner
}
}
Ready to Level Up Your Skills?
Explore 500+ free tutorials across 20+ languages and frameworks.