JVM, IntelliJ Setup & Hello World
JVM, IntelliJ Setup & Hello World
What Is the JVM and Why Does It Matter?
Java's core promise is write once, run anywhere. You write Java source code (.java), the compiler (javac) turns it into bytecode (.class), and the JVM runs that bytecode on any machine — Windows, macOS, Linux — without recompiling. The JVM also handles memory management through garbage collection, so you never call free() like in C.
The JVM ecosystem is massive. Java runs Android apps, the bulk of enterprise backends, distributed systems at Google and LinkedIn, and scientific computing pipelines. Kotlin — which we cover in Module 7 — also compiles to JVM bytecode, making everything you learn here directly transferable.
---
Installing JDK 21 (LTS)
JDK 21 is the current long-term support release. Always use an LTS version for serious projects.
macOS:
brew install --cask temurin@21
java -version # should print: openjdk 21.x.xWindows: Download the .msi installer from https://adoptium.net — select JDK 21, Windows, x64. Run it. Open a new terminal and verify:
java -version
javac -versionLinux (Ubuntu/Debian):
sudo apt update
sudo apt install temurin-21-jdk
java -version---
IntelliJ IDEA Setup
IntelliJ is the professional IDE for Java. The Community Edition is free and has everything you need.
- Download IntelliJ IDEA Community from jetbrains.com/idea
- Open it → New Project → Java → select JDK 21
- Name the project: JavaCrashCourse
- IntelliJ creates the project structure: src/ for source files, out/ for compiled bytecode
Alternatively, VS Code works well with the "Extension Pack for Java" (Microsoft). Install it and point it to your JDK 21 installation. For this course, either IDE is fine.
---
The Compile–Run Cycle
HelloWorld.java → javac HelloWorld.java → HelloWorld.class → java HelloWorld
source compiler bytecode JVM runs itIn IntelliJ, clicking the green Run button does both steps automatically. Understanding what happens under the hood matters when debugging classpath errors.
---
Anatomy of a Java Program
package com.jst.crash; // optional: the package this class belongs to
public class HelloWorld { // class name MUST match filename: HelloWorld.java
public static void main(String[] args) { // entry point
System.out.println("Hello, World!");
// args contains command-line arguments: java HelloWorld Alice
if (args.length > 0) {
System.out.println("Hello, " + args[0] + "!");
}
}
}Key rules:
- One public class per file
- Class name must exactly match the filename (case-sensitive)
- main must be
public static voidwithString[] args - Statements end with semicolons
---
Your First Real Program: A Temperature Converter
public class TempConverter {
// static method: belongs to the class, not an instance
public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9.0 / 5.0) + 32;
}
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5.0 / 9.0;
}
public static void main(String[] args) {
double boiling = 100.0;
double bodyTemp = 98.6;
System.out.printf("%.1f°C = %.1f°F%n",
boiling, celsiusToFahrenheit(boiling));
System.out.printf("%.1f°F = %.1f°C%n",
bodyTemp, fahrenheitToCelsius(bodyTemp));
}
}System.out.printf uses format strings: %.1f means a floating-point number with 1 decimal place. %n is a platform-independent newline (better than \n in printf).
---
Static vs. Instance Members
Everything in the main method above is static — it belongs to the class itself, not to any object. You call static methods with: ClassName.methodName() or just methodName() from within the same class.
In Object-Oriented Java (Modules 3–5), you'll create instances of classes — objects that hold their own state. Static is the entry point; instances are the architecture.
---
String Basics
String name = "Jordan";
int len = name.length(); // 6
String upper = name.toUpperCase(); // "JORDAN"
String sub = name.substring(0, 3); // "Jor"
boolean starts = name.startsWith("J"); // true
// String concatenation
String greeting = "Hello, " + name + "!";
// String formatting (preferred over concatenation for complex strings)
String msg = String.format("Name: %s, Length: %d", name, len);
System.out.println(msg);Strings in Java are immutable — every operation creates a new String object. For heavy string building, use StringBuilder.