V
Vel·ToolKit
Simple · Fast · Ready to use
EN
Chapter 1 of 20

Getting Started

Intro to Java, installing the JDK, and your first program

What is Java

Java is an object-oriented programming language released in 1995. Its defining feature is "compile once, run anywhere": source code is first compiled to bytecode (.class), which the JVM (Java Virtual Machine) interprets on different operating systems — the same bytecode runs on Windows, Linux, and macOS.

Java is strongly and statically typed, with automatic garbage collection and powerful runtime reflection. It is the mainstream language for enterprise backend services (the Spring ecosystem), Android apps, and big data (Hadoop / Spark / Flink).

JDK vs JRE vs JVM

  • JVM: the Java Virtual Machine — the core that runs bytecode and the key to cross-platform support
  • JRE: JVM + standard library — can only run Java programs, cannot compile
  • JDK: JRE + compiler (javac) + toolchain (jar/javadoc/jdb, etc.) — required for development

Installing JDK 17

Recommended distributions: Oracle JDK, Eclipse Temurin (the popular community build, free for commercial use), Amazon Corretto, Alibaba Dragonwell. After installing, configure JAVA_HOME and PATH, then verify:

$ java -version
openjdk version "17.0.10" 2024-01-16
OpenJDK Runtime Environment Temurin-17.0.10+7 (build 17.0.10+7)
OpenJDK 64-Bit Server VM Temurin-17.0.10+7 (build 17.0.10+7, mixed mode)

$ javac -version
javac 17.0.10

Hello World

Create a file Hello.java. Note: the name of a public class must exactly match the file name (case-sensitive).

// Hello.java
public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Compile and Run

The traditional two steps: javac compiles to .class bytecode, then java loads and runs it. Note that the java command takes a "class name", not a ".class file name".

$ javac Hello.java       # produces Hello.class
$ java Hello             # run it (note: no .class suffix)
Hello, World!

Running a Single Source File Directly (Java 11+)

Since Java 11 you can run java Hello.java directly without compiling with javac first. Handy for small scripts:

$ java Hello.java
Hello, World!

The main Method Signature

On startup the JVM calls the class's public static void main(String[] args) method. The signature is fixed: public, static, returns void, parameter is a String array. args holds the command-line arguments.

// Greet.java
public class Greet {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Hello, World!");
        } else {
            System.out.println("Hello, " + args[0] + "!");
        }
    }
}
$ javac Greet.java
$ java Greet Alice
Hello, Alice!

Starting a Project with Maven

Real-world development uses Maven or Gradle to manage dependencies and builds. Scaffolding a new project with Maven:

$ mvn archetype:generate \
  -DgroupId=com.example \
  -DartifactId=myapp \
  -DarchetypeArtifactId=maven-archetype-quickstart \
  -DarchetypeVersion=1.4 \
  -DinteractiveMode=false
$ cd myapp
$ mvn compile
$ mvn exec:java -Dexec.mainClass=com.example.App

The generated standard directory layout:

myapp/
├── pom.xml                       # project descriptor + dependencies
└── src/
    ├── main/
    │   └── java/
    │       └── com/example/App.java
    └── test/
        └── java/
            └── com/example/AppTest.java