8 个基本类型
| 类型 | 字节 | 范围 |
|---|---|---|
byte |
1 | -128 ~ 127 |
short |
2 | -32768 ~ 32767 |
int |
4 | ±21 亿 |
long |
8 | ±9.2 × 10^18 |
float |
4 | IEEE 单精度 |
double |
8 | IEEE 双精度 |
boolean |
1 (jvm 视情) | true / false |
char |
2 | UTF-16 单元 |
int n = 42;
long big = 100_000_000_000L; // L 后缀
float f = 3.14f; // f 后缀
double d = 3.14; // 默认 double
boolean ok = true;
char c = 'A'; // 单引号
注意 long 加 L、float 加 f——否则编译错或精度问题。
字面量分隔符
int million = 1_000_000; // 下划线增强可读
long mac = 0xCAFE_F00DL;
byte mask = 0b0011_1010; // 二进制
包装类(Wrapper)
每个基本类型有对应"对象"版本:
| 基本 | 包装 |
|---|---|
| int | Integer |
| long | Long |
| double | Double |
| boolean | Boolean |
| char | Character |
| ... | ... |
int x = 5;
Integer obj = x; // 自动装箱:int → Integer
int y = obj; // 自动拆箱:Integer → int
什么时候用包装类
- 泛型必须:
List<Integer>,不能List<int> - 可空:基本类型不能 null,
Integer i = null;行 - 方法工具:
Integer.parseInt("42")/Integer.MAX_VALUE
List<Integer> nums = new ArrayList<>(); // 必须 Integer
nums.add(1); // 自动装箱
Integer val = map.get("key");
if (val == null) ... // 找不到时
自动装拆箱的坑
Integer a = 100;
Integer b = 100;
System.out.println(a == b); // true(小整数缓存)
Integer c = 200;
Integer d = 200;
System.out.println(c == d); // false(不同对象)
System.out.println(c.equals(d)); // true(用 equals 比值)
== 比对象引用、equals() 比值——这是包装类型的常见陷阱。
-128 ~ 127 之间的 Integer 是缓存的(同一对象),之外是新对象。
var 推断(Java 10+)
var n = 42; // int
var s = "hello"; // String
var list = new ArrayList<String>(); // ArrayList<String>
var map = new HashMap<String, Integer>();
// 老写法
String s = "hello";
ArrayList<String> list = new ArrayList<String>();
HashMap<String, Integer> map = new HashMap<String, Integer>();
只在局部变量——字段、参数、返回类型不能用。
注意:
var nothing = null; // ❌ 错误:var 不知类型
var x = null; // ❌
var generic = new ArrayList<>(); // List<Object>——可能不是你想要的
字符串转数字
int n = Integer.parseInt("42"); // 失败抛 NumberFormatException
double d = Double.parseDouble("3.14");
// 安全
try {
int n = Integer.parseInt(s);
} catch (NumberFormatException e) {
n = 0;
}
// Optional(Java 9+)
OptionalInt parsed = Optional.ofNullable(s)
.map(str -> {
try { return Integer.parseInt(str); }
catch (NumberFormatException e) { return null; }
})
.map(OptionalInt::of)
.orElse(OptionalInt.empty());
数字转字符串
int n = 42;
String s1 = Integer.toString(n); // "42"
String s2 = String.valueOf(n); // "42"
String s3 = "" + n; // "42" (但 hack 风格不推荐)
String s4 = String.format("%d", n);
String s5 = "%d".formatted(n); // Java 15+
类型转换
隐式(自动)
int n = 10;
long l = n; // int → long 自动
double d = l; // long → double 自动
窄类型 → 宽类型自动。
显式(强制)
long l = 10000000000L;
int n = (int) l; // 截断到 int
double d = 3.7;
int x = (int) d; // 3(截断小数)
宽 → 窄需要显式 (int) 等——可能丢精度。
char 与 int
char c = 'A';
int code = c; // 65(隐式转 int)
char back = (char) 66; // 'B'
if (c >= 'a' && c <= 'z') ... // 字符范围
char 是 16 位无符号——表示 UTF-16 code unit(不是 code point!)。中文 / emoji 可能占 2 个 char。
BigInteger / BigDecimal
需要超过 long 的范围 / 精确小数(金额):
import java.math.BigInteger;
import java.math.BigDecimal;
BigInteger huge = new BigInteger("99999999999999999999999999999");
BigInteger sum = huge.add(BigInteger.ONE); // 必须用方法
BigDecimal price = new BigDecimal("19.99"); // 用 String 构造避免浮点误差
BigDecimal tax = price.multiply(new BigDecimal("0.08"));
金额永远用 BigDecimal——double 0.1 + 0.2 ≠ 0.3 的精度问题,钱算错你赔不起。
心智模型
- 本地变量、循环计数、临时数学 → 基本类型(int / long / double)
- 集合元素、可能 null、API 参数 → 包装类(Integer / Long / Double)
- 大数 / 金额 → BigInteger / BigDecimal
- 类型明显 →
var,否则写出来更清楚
→ 下一篇 字符串