Java 教程
写出并运行第一个 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.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("Hello, Java!");
}
}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.
$ javac HelloWorld.java # 生成 HelloWorld.class
$ java HelloWorld # 运行,输出两行
Hello, World!
Hello, Java!javac— the compiler, source to bytecodejava— the launcher, runs a class in the JVMmain— 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.
- Syntax and types — the grammar and the eight primitives
- Control flow — if / switch / while / for
- OOP — encapsulation, inheritance, polymorphism, interfaces
- Collections and concurrency — the parts real projects need
我的第一个 JAVA 程序
Java 的一切代码都写在类里,文件名必须与 public class 的类名完全一致(含大小写)。程序入口固定是 main 方法,JVM 从这里开始执行。
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("Hello, Java!");
}
}Hello, World!
Hello, Java!编译与运行
javac HelloWorld.java 把源码编译成字节码 HelloWorld.class,java HelloWorld 启动 JVM 执行它。注意运行时写的是类名,不带 .class 后缀。
$ javac HelloWorld.java # 生成 HelloWorld.class
$ java HelloWorld # 运行,输出两行
Hello, World!
Hello, Java!javac—— 编译器,源码编译成字节码java—— 启动器,在 JVM 中运行类main—— JVM 唯一寻找的入口
后续学习路径
按左侧目录顺序往下读即可:基础语法 → 对象和类 → 数据类型与变量 → 运算符与流程控制 → 字符串与数组 → 面向对象三大特性 → 集合与泛型 → 并发与 IO。每章都配可运行示例。
- 语法与类型 —— 语法规则与八种基本类型
- 流程控制 —— if / switch / while / for
- 面向对象 —— 封装、继承、多态、接口
- 集合与并发 —— 真实项目最常用的部分