Java 基础教程
知识结构
第一章: Java 入门
1.1 什么是 Java
Java 是一门面向对象的编程语言,由 Sun Microsystems(现属于 Oracle)于 1995 年发布。Java 的设计理念是”一次编写,到处运行”(Write Once, Run Anywhere),这意味着 Java 程序可以在任何安装了 Java 虚拟机(JVM)的平台上运行。
Java 的特点:
- 跨平台性:通过 JVM 实现跨平台运行
- 面向对象:支持封装、继承、多态
- 安全性:内置安全机制,如自动内存管理
- 健壮性:强类型检查、异常处理机制
- 多线程:内置多线程支持
1.2 JDK、JRE 和 JVM 的关系
理解这三个概念是学习 Java 的第一步:
+------------------------------------------+| JDK || +------------------------------------+ || | JRE | || | +------------------------------+ | || | | JVM | | || | | - 执行字节码 | | || | | - 内存管理 | | || | | - 垃圾回收 | | || | +------------------------------+ | || | + 核心类库 (rt.jar) | | || +------------------------------------+ || + 开发工具 (javac, java, jar...) |+------------------------------------------+- JVM(Java Virtual Machine):Java 虚拟机,负责执行 Java 字节码
- JRE(Java Runtime Environment):Java 运行时环境 = JVM + 核心类库,用于运行 Java 程序
- JDK(Java Development Kit):Java 开发工具包 = JRE + 开发工具,用于开发 Java 程序
简单记忆:开发 Java 程序需要 JDK,运行 Java 程序需要 JRE
1.3 Java 程序的编译与运行
Java 程序从源代码到执行经历以下步骤:
源代码(.java) --> 编译(javac) --> 字节码(.class) --> 执行(java/JVM) --> 结果- 编写:创建
.java源文件 - 编译:使用
javac命令将源文件编译为.class字节码文件 - 运行:使用
java命令让 JVM 执行字节码
1.4 Hello World
让我们编写第一个 Java 程序:
Loading CheerpJ...
First load requires ~30MB download
代码解析:
public class Main { // 定义一个公共类,类名必须与文件名相同 public static void main(String[] args) { // 主方法,程序入口 System.out.println("Hello, World!"); // 输出语句 }}public class Main:定义一个名为Main的公共类public static void main(String[] args):主方法,程序从这里开始执行public:公共的,任何地方都可以访问static:静态的,不需要创建对象就可以调用void:无返回值main:方法名,JVM 约定的入口方法名String[] args:命令行参数数组
System.out.println():打印输出并换行
第二章: 数据类型
Java 是强类型语言,每个变量在使用前必须声明其类型。Java 的数据类型分为两大类:基本类型和引用类型。
2.1 基本数据类型
Java 有 8 种基本数据类型:
| 类型 | 大小 | 范围 | 默认值 | 说明 |
|---|---|---|---|---|
byte | 1字节 | -128 ~ 127 | 0 | 最小的整数类型 |
short | 2字节 | -32768 ~ 32767 | 0 | 短整型 |
int | 4字节 | -2^31 ~ 2^31-1 | 0 | 最常用的整数类型 |
long | 8字节 | -2^63 ~ 2^63-1 | 0L | 大整数,需加 L 后缀 |
float | 4字节 | 约 ±3.4E38 | 0.0f | 单精度浮点,需加 F 后缀 |
double | 8字节 | 约 ±1.8E308 | 0.0d | 双精度浮点,默认浮点类型 |
char | 2字节 | 0 ~ 65535 | ’\u0000’ | Unicode 字符 |
boolean | 1位 | true / false | false | 布尔值 |
Loading CheerpJ...
First load requires ~30MB download
2.2 引用数据类型
引用类型包括:类(Class)、接口(Interface)、数组(Array)。最常用的引用类型是 String。
Loading CheerpJ...
First load requires ~30MB download
2.3 类型转换
自动类型转换(隐式转换)
当把小范围类型赋值给大范围类型时,Java 会自动转换:
byte -> short -> int -> long -> float -> double char -^Loading CheerpJ...
First load requires ~30MB download
强制类型转换(显式转换)
当把大范围类型赋值给小范围类型时,需要强制转换,可能会丢失精度:
Loading CheerpJ...
First load requires ~30MB download
2.4 变量与常量
Loading CheerpJ...
First load requires ~30MB download
第三章: 运算符
3.1 算术运算符
Loading CheerpJ...
First load requires ~30MB download
3.2 关系运算符
Loading CheerpJ...
First load requires ~30MB download
3.3 逻辑运算符
Loading CheerpJ...
First load requires ~30MB download
3.4 位运算符
Loading CheerpJ...
First load requires ~30MB download
3.5 赋值运算符与三元运算符
Loading CheerpJ...
First load requires ~30MB download
第四章: 控制流
4.1 if-else 条件语句
Loading CheerpJ...
First load requires ~30MB download
4.2 switch 语句
Loading CheerpJ...
First load requires ~30MB download
4.3 for 循环
Loading CheerpJ...
First load requires ~30MB download
4.4 while 和 do-while 循环
Loading CheerpJ...
First load requires ~30MB download
4.5 for-each 循环
Loading CheerpJ...
First load requires ~30MB download
4.6 break 和 continue
Loading CheerpJ...
First load requires ~30MB download
第五章: 数组
5.1 一维数组
Loading CheerpJ...
First load requires ~30MB download
5.2 多维数组
Loading CheerpJ...
First load requires ~30MB download
第六章: 方法
6.1 方法定义与调用
Loading CheerpJ...
First load requires ~30MB download
6.2 方法重载
方法重载(Overload)是指在同一个类中,方法名相同但参数列表不同。
Loading CheerpJ...
First load requires ~30MB download
6.3 可变参数
Loading CheerpJ...
First load requires ~30MB download
6.4 递归
Loading CheerpJ...
First load requires ~30MB download
6.5 参数传递
Loading CheerpJ...
First load requires ~30MB download
第七章: 面向对象编程
7.1 类与对象
Loading CheerpJ...
First load requires ~30MB download
7.2 封装
Loading CheerpJ...
First load requires ~30MB download
7.3 继承
Loading CheerpJ...
First load requires ~30MB download
7.4 多态
Loading CheerpJ...
First load requires ~30MB download
7.5 静态成员
Loading CheerpJ...
First load requires ~30MB download
第八章: 接口与抽象类
8.1 抽象类
Loading CheerpJ...
First load requires ~30MB download
8.2 接口
Loading CheerpJ...
First load requires ~30MB download
8.3 抽象类 vs 接口
Loading CheerpJ...
First load requires ~30MB download
第九章: 异常处理
9.1 异常体系
Throwable├── Error (严重错误,不应捕获)│ ├── OutOfMemoryError│ ├── StackOverflowError│ └── ...└── Exception (可处理的异常) ├── RuntimeException (运行时异常,非检查异常) │ ├── NullPointerException │ ├── ArrayIndexOutOfBoundsException │ ├── ArithmeticException │ └── ... └── 其他 Exception (检查异常) ├── IOException ├── SQLException └── ...9.2 try-catch-finally
Loading CheerpJ...
First load requires ~30MB download
9.3 throw 和 throws
Loading CheerpJ...
First load requires ~30MB download
第十章: 内部类
Java 支持在一个类的内部定义另一个类,称为内部类(Inner Class)。内部类可以访问外部类的所有成员,包括私有成员。
10.1 成员内部类
Loading CheerpJ...
First load requires ~30MB download
10.2 静态内部类
Loading CheerpJ...
First load requires ~30MB download
10.3 局部内部类
Loading CheerpJ...
First load requires ~30MB download
10.4 匿名内部类
Loading CheerpJ...
First load requires ~30MB download
10.5 内部类总结
| 类型 | 位置 | 能否访问外部类成员 | 能否有 static 成员 | 创建方式 |
|---|---|---|---|---|
| 成员内部类 | 类内部 | 所有成员 | 不能 | outer.new Inner() |
| 静态内部类 | 类内部(static) | 仅静态成员 | 能 | new Outer.Inner() |
| 局部内部类 | 方法内部 | 所有成员 + final局部变量 | 不能 | 方法内部 new |
| 匿名内部类 | 表达式中 | 所有成员 + final局部变量 | 不能 | new Interface/Class() |
第十一章: 泛型
11.1 泛型类
Loading CheerpJ...
First load requires ~30MB download
11.2 泛型方法
Loading CheerpJ...
First load requires ~30MB download
11.3 类型通配符
Loading CheerpJ...
First load requires ~30MB download
第十二章: 集合框架
12.1 List 接口
Loading CheerpJ...
First load requires ~30MB download
12.2 Set 接口
Loading CheerpJ...
First load requires ~30MB download
12.3 Map 接口
Loading CheerpJ...
First load requires ~30MB download
12.4 Queue 和 Deque
Loading CheerpJ...
First load requires ~30MB download
第十三章: Lambda 表达式
13.1 函数式接口
Loading CheerpJ...
First load requires ~30MB download
13.2 常用函数式接口
Loading CheerpJ...
First load requires ~30MB download
13.3 方法引用
Loading CheerpJ...
First load requires ~30MB download
第十四章: Stream API
14.1 创建 Stream
Loading CheerpJ...
First load requires ~30MB download
14.2 中间操作
Loading CheerpJ...
First load requires ~30MB download
14.3 终端操作
Loading CheerpJ...
First load requires ~30MB download
14.4 Collectors 工具类
Loading CheerpJ...
First load requires ~30MB download
第十五章: 常用类
15.1 String 与 StringBuilder
Loading CheerpJ...
First load requires ~30MB download
15.2 包装类
Loading CheerpJ...
First load requires ~30MB download
15.3 日期时间 API (Java 8+)
Loading CheerpJ...
First load requires ~30MB download
总结
本教程涵盖了 Java 编程语言的核心知识:
- 基础语法:数据类型、运算符、控制流、数组、方法
- 面向对象:类与对象、封装、继承、多态、接口与抽象类
- 异常处理:try-catch-finally、自定义异常
- 泛型:泛型类、泛型方法、类型通配符
- 集合框架:List、Set、Map、Queue
- 函数式编程:Lambda 表达式、方法引用
- Stream API:创建流、中间操作、终端操作
- 常用类:String、包装类、日期时间 API
掌握这些核心概念后,你就拥有了 Java 编程的坚实基础,可以进一步学习更高级的主题,如多线程、网络编程、数据库操作等。