Java 教程

写出并运行第一个 Java 程序,理解源码到字节码的编译流程。

Beginner
Key PointsJava 程序以类为单位,入口是 public static void main(String[] args);先 javac 编译成 .class,再用 java 运行。

我的第一个 JAVA 程序

All Java code lives inside a class, and the file name must match the public class name exactly, case included. Execution always starts at the main method.

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

编译与运行

javac HelloWorld.java compiles to bytecode HelloWorld.class; java HelloWorld starts the JVM to run it. At run time you pass the class name without the .class suffix.

Text
$ javac HelloWorld.java     # 生成 HelloWorld.class
$ java HelloWorld           # 运行,输出两行
Hello, World!
Hello, Java!
  • javac — the compiler, source to bytecode
  • java — the launcher, runs a class in the JVM
  • main — the only entry point the JVM looks for

后续学习路径

Follow the left-hand outline: basic syntax → objects and classes → types and variables → operators and control flow → strings and arrays → the three OOP pillars → collections and generics → concurrency and IO. Every chapter ships runnable examples.

  1. Syntax and types — the grammar and the eight primitives
  2. Control flow — if / switch / while / for
  3. OOP — encapsulation, inheritance, polymorphism, interfaces
  4. Collections and concurrency — the parts real projects need

Friends & Partners