Introduction to Java
Get started with Java by understanding the JVM architecture, installing the JDK, and compiling your first program.
The Java Virtual Machine
Java is a statically typed, object-oriented programming language designed to run on the Java Virtual Machine (JVM). The JVM provides platform independence by executing compiled bytecode, fulfilling Java's promise of write once, run anywhere. When you compile a Java source file, the compiler produces a .class file containing bytecode rather than machine-specific instructions. This architecture also enables other languages like Kotlin and Scala to run on the same virtual machine.
Installing the JDK
The Java Development Kit (JDK) includes the compiler, runtime, and standard library needed to develop Java applications. You can download the JDK from Oracle or use open-source distributions like Eclipse Adoptium or Amazon Corretto. After installation, verify your setup by checking the java and javac commands in your terminal. Setting the JAVA_HOME environment variable ensures build tools like Maven and Gradle can locate your JDK installation.
# Verify Java installation
java --version
# Verify the compiler
javac --version
# Set JAVA_HOME (bash/zsh)
export JAVA_HOME=$(/usr/libexec/java_home)
# Compile and run a Java file
javac HelloWorld.java
java HelloWorld
Hello World in Java
Every Java application must contain at least one class with a main method that serves as the entry point. The main method has a specific signature that the JVM looks for when launching the program. Java requires that the public class name matches the filename, so a class named HelloWorld must be in a file called HelloWorld.java. The System.out.println method writes output to the console followed by a newline character.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
// Print with formatting
String language = "Java";
int version = 21;
System.out.printf("Welcome to %s %d!%n", language, version);
// String concatenation
System.out.println("Java " + "is " + "awesome!");
}
}
Compilation and Execution
The Java compilation process involves two distinct steps: compiling source code to bytecode and executing the bytecode on the JVM. The javac compiler checks for syntax errors and type correctness at compile time, catching many bugs before the program runs. Since Java 11, you can run single-file programs directly with java without explicitly compiling first. Understanding the compilation model helps you diagnose build errors and appreciate Java's type safety guarantees.
// Save as Greeter.java
public class Greeter {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Hello, " + args[0] + "!");
} else {
System.out.println("Hello, stranger!");
}
// Print all command-line arguments
for (int i = 0; i < args.length; i++) {
System.out.println("Arg " + i + ": " + args[i]);
}
}
}
// Compile: javac Greeter.java
// Run: java Greeter Alice Bob
// Or (Java 11+): java Greeter.java Alice Bob