Java 是由 Sun Microsystems(现属 Oracle)于 1995 年发布的编程语言与计算平台。凭借跨平台、面向对象、自动内存管理、丰富生态四大核心优势,长期占据 TIOBE 排行榜前三,成为企业级开发、Android 开发、大数据、云原生微服务的首选语言。
「一次编写,到处运行」 —— Java 字节码 + JVM 抽象了底层操作系统与硬件差异,实现了真正的平台无关性。
| 特性 | 说明 |
|---|---|
| 面向对象 | 封装、继承、多态,代码复用与维护的基石 |
| 跨平台 | 源码 → 字节码 → JVM 解释/编译执行,屏蔽 OS/硬件差异 |
| 自动垃圾回收 (GC) | 托管内存,消除手动 malloc/free,防止内存泄漏与悬空指针 |
| 多线程内置支持 | Thread、并发包、锁、线程池,原生支持高并发编程 |
| 安全性 | 字节码校验、类加载器隔离、安全管理器、无指针运算 |
| 泛型/注解/Lambda/模块化 | 现代语言特性持续演进 (Java 5/8/9/11/17/21 LTS) |
学习路径建议:Java SE 核心 → Java Web (Servlet/JSP/Filter) → Spring 生态 → 中间件/分布式/架构
JDK (Java Development Kit) = JRE + 开发工具 (javac, jdb, jconsole, jvisualvm, jcmd...)
│
└── JRE (Java Runtime Environment) = JVM + 核心类库 (rt.jar/modules)
│
└── JVM (Java Virtual Machine) = 类加载器 + 运行时数据区 + 执行引擎 + 本地库接口
javac HelloWorld.java # 编译:.java → .class (字节码)
java HelloWorld # 运行:启动 JVM → 加载类 → 执行 main 方法
# 入口必须为:public static void main(String[] args)
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
class:类声明,编译单元public static void main:JVM 识别的启动入口System.out.println:标准输出流打印| 类别 | 类型 | 位数 | 默认值 | 取值范围 / 说明 |
|---|---|---|---|---|
| 逻辑 | boolean | 1 位 (JVM 实现通常 1 字节) | false | true / false |
| 文本 | char | 16 位 | '\u0000' | Unicode 字符,0 ~ 65535 |
| 整数 | byte | 8 位 | 0 | -128 ~ 127 |
| short | 16 位 | 0 | -32,768 ~ 32,767 | |
| int | 32 位 | 0 | -2^31 ~ 2^31-1 | |
| long | 64 位 | 0L | -2^63 ~ 2^63-1 | |
| 浮点 | float | 32 位 | 0.0f | 单精度,~7 位有效数字,后缀 f/F |
| double | 64 位 | 0.0 | 双精度,~15 位有效数字,默认实数类型 |
float f = 1.3; ❌ 编译报错 → float f = 1.3f; ✅long l = 8888888888; ❌ → long l = 8888888888L; ✅byte a=1, b=2; byte c = a + b; ❌ → byte c = (byte)(a + b); ✅0123、十六进制 0xFF、二进制 0b1010 (Java 7+)\n 换行、\t 制表、\r 回车、\\ 反斜杠、\" 双引号、\' 单引号byte → short/char → int → long → float → double
// 容量小 → 容量大,精度不降低
// byte/short/char 互不相互提升,参与运算先转 int
double d = 3.14;
int i = (int) d; // 3,截断小数,可能溢出
long l = 300;
byte b = (byte) l; // 44 (300 % 256),高位截断
| 分类 | 运算符 | 要点 |
|---|---|---|
| 算术 | + - * / % ++ -- | ++/-- 前缀先算后用,后缀先用后算 |
| 赋值 | = += -= *= /= %= | 复合赋值隐含强转:byte b=1; b+=2; 等价 b=(byte)(b+2) |
| 关系 | > < >= <= == != | 结果为 boolean,不可写 1 < x < 10 |
| 逻辑 | ! & | ^ && || | && 短路与,|| 短路或,^ 异或 |
| 位运算 | & | ^ ~ >> << >>> | >> 带符号右移,>>> 无符号右移 (补 0) |
| 三元 | 条件 ? 值1 : 值2 | 类型向上兼容:false ? 'a' : 8 → int 8 |
// for
for (int i = 0; i < 10; i++) { ... }
// 增强 for / for-each (Java 5+)
for (int num : array) { ... }
// while
while (condition) { ... }
// do-while (至少执行一次)
do { ... } while (condition);
// switch (Java 12+ 支持 switch 表达式、yield)
switch (day) {
case 1, 2, 3, 4, 5 -> System.out.println("工作日");
case 6, 7 -> System.out.println("周末");
default -> System.out.println("错误");
}
break:跳出最近循环/switchcontinue:跳过本次循环,进入下次判断return:退出方法Integer.parseInt("123") // 字符串转 int
Double.valueOf(3.14).intValue() // 包装类转基本类型
Math.max/min(a, b) // 最大/最小
Math.abs/ceil/floor/round // 绝对值/向上/向下/四舍五入取整
Math.sqrt/pow/log/exp // 根/幂/对数/指数
Math.sin/cos/tan // 三角函数 (参数为弧度)
Math.random() // [0, 1) 随机数
(int)(Math.random() * 100) // [0, 99] 随机整数
Character.isLetter/digit/whitespace/UpperCase/LowerCase(c)
Character.toUpperCase/toLowerCase(c)
Character.toString(c)
"abc" (常量池) vs new String("abc") (堆)length()、charAt(i)、substring(begin, end)、indexOf/lastIndexOf、split(regex)、replace/replaceAll、toUpperCase/toLowerCase、trim()、equals/equalsIgnoreCaseStringBuilder,循环拼接手动用 StringBuilderappend/insert/delete/replace/reverseStringBuilder:非线程安全,高性能,首选StringBuffer:线程安全 (synchronized),较慢int[] arr = new int[5];
int[] arr2 = {1, 2, 3, 4, 5};
arr[0] = 10;
Arrays.sort(arr); // 排序
Arrays.binarySearch(arr, 3); // 二分查找 (需有序)
Arrays.fill(arr, 9); // 填充
Arrays.equals(arr1, arr2); // 比较
// 多维数组:int[][] matrix = new int[3][4];
LocalDate.now() // 当前日期
LocalTime.now() // 当前时间
LocalDateTime.now() // 日期时间
Instant.now() // 时间戳 (UTC)
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(ldt)
Duration.between(start, end).toMillis() // 毫秒差
Period.between(d1, d2).getDays() // 天数差
// 旧 API Date/Calendar/SimpleDateFormat 已不推荐使用
private 字段 + public getter/setterextends 单继承,implements 多实现super 访问父类成员,@Override 重写注解super()Animal a = new Dog();Throwable
├── Error (严重错误,不可恢复:OOM, StackOverflow)
└── Exception
├── RuntimeException (非受检:NPE, IndexOutOfBounds, IllegalArgument)
└── 受检异常 (IOException, SQLException, ClassNotFoundException)
try {
// リスクコード
} catch (SpecificException e) {
// 处理特定异常
} catch (Exception e) {
// 兜底
} finally {
// 资源释放 (JDK 7+ try-with-resources 自动关闭 AutoCloseable)
}
Integer i = 100; // 自动装箱: Integer.valueOf(100) [-128, 127] 缓存
int n = i; // 自动拆箱: i.intValue()
Integer a = 100, b = 100;
System.out.println(a == b); // true (缓存命中)
Integer c = 200, d = 200;
System.out.println(c == d); // false (新对象,比较引用)
System.out.println(c.equals(d)); // true (比较值)
本文构建了 Java 入门的核心知识骨架:环境、语法、类型、流程、核心 API、OO 基础、异常、包装类。建议后续按以下路径深入:
代码不在于多,在于精。每个知识点都要能写出正确、规范、可运行的示例代码。动手敲代码,才是通往 Java 精通的唯一捷径。