java.lang.Long
是 long
基本数据类型的包装类。它提供了多个方法用于将 Long
对象转换为其他基本数据类型,如 long
, int
, byte
, short
, float
, double
。这些方法在处理数值转换、集合操作(如 List<Long>
转基本类型数组)时非常常见。
一、方法定义
方法名 | 定义 |
---|---|
long longValue() |
返回该 Long 对象表示的 long 值。 |
int intValue() |
将该 Long 对象的值转换为 int 后返回。 |
byte byteValue() |
将该 Long 对象的值转换为 byte 后返回。 |
short shortValue() |
将该 Long 对象的值转换为 short 后返回。 |
float floatValue() |
将该 Long 对象的值转换为 float 后返回。 |
double doubleValue() |
将该 Long 对象的值转换为 double 后返回。 |
所有方法均继承自抽象类
java.lang.Number
。
二、功能说明
方法 | 功能 |
---|---|
longValue() |
安全转换,无精度损失(原始类型即为 long ) |
intValue() |
强制截断为 32 位整数,可能溢出 |
byteValue() |
强制截断为 8 位整数,严重溢出风险 |
shortValue() |
强制截断为 16 位整数,溢出风险 |
floatValue() |
转换为单精度浮点数,可能丢失精度(尤其大整数) |
doubleValue() |
转换为双精度浮点数,精度较高,大整数也可能轻微损失 |
⚠️ 除
longValue()
外,其余方法均可能导致数据截断或精度丢失。
三、示例代码
public class LongConversionExample {
public static void main(String[] args) {
Long value = 1234567890123L;
// 1. longValue()
long l = value.longValue();
System.out.println("longValue(): " + l); // 1234567890123
// 2. intValue() - 截断示例
int i = value.intValue();
System.out.println("intValue(): " + i); // -1068242133 (溢出)
// 3. shortValue()
short s = value.shortValue();
System.out.println("shortValue(): " + s); // 2733 (高位截断)
// 4. byteValue()
byte b = value.byteValue();
System.out.println("byteValue(): " + b); // 77
// 5. floatValue()
float f = value.floatValue();
System.out.println("floatValue(): " + f); // 1.23456794E12 (精度丢失)
// 6. doubleValue()
double d = value.doubleValue();
System.out.println("doubleValue(): " + d); // 1.234567890123E12
}
}
输出结果:
longValue(): 1234567890123
intValue(): -1068242133
shortValue(): 2733
byteValue(): 77
floatValue(): 1.23456794E12
doubleValue(): 1.234567890123E12
四、使用技巧
自动拆箱替代调用
Java 5+ 支持自动拆箱,以下等价:Long num = 100L; long l = num; // 自动调用 longValue() int i = num.intValue(); // 显式调用
集合中批量转换
List<Long> longList = Arrays.asList(1L, 2L, 3L); int[] intArray = longList.stream().mapToInt(Long::intValue).toArray();
浮点转换用于科学计算
double d = someLong.doubleValue() / 3.0; // 避免整数除法
位运算前转换
byte b = value.byteValue(); int masked = b & 0xFF; // 处理符号扩展
五、常见错误
错误 | 说明 |
---|---|
数值溢出 | intValue() 将大于 Integer.MAX_VALUE 的 Long 转换导致负数 |
精度丢失 | floatValue() 对大整数(> 2^24)丢失最后几位 |
空指针异常 | 对 null 的 Long 对象调用方法:NullPointerException |
误解返回类型 | 误以为 byteValue() 返回无符号值(实际为有符号 byte ) |
示例错误代码:
Long nullLong = null;
int bad = nullLong.intValue(); // 抛出 NullPointerException
六、注意事项
- ✅
longValue()
是唯一无损转换方法,其他都可能丢失信息。 - ⚠️ 溢出无警告:Java 不会抛出异常,而是静默截断(补码环绕)。
- ⚠️
float
精度有限:long
最多 19 位十进制数,float
仅保证 6-7 位有效数字。 - ⚠️
byte
、short
范围极小,极易溢出,使用前务必校验范围。 - ❗ 空值检查:在调用任何
xxxValue()
方法前,确保Long
对象非null
。
七、最佳实践
优先使用自动拆箱
long l = myLong; // 比 myLong.longValue() 更简洁
转换前校验范围
public static int safeIntValue(Long value) { if (value == null) return 0; if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) { throw new IllegalArgumentException("Long value out of int range: " + value); } return value.intValue(); }
大数浮点转换优先用
doubleValue()
double d = bigLong.doubleValue(); // 比 floatValue() 更精确
避免无意义转换
// ❌ 不必要 byte b = myLong.byteValue(); // ✅ 明确需求再转换 if (myLong >= Byte.MIN_VALUE && myLong <= Byte.MAX_VALUE) { byte b = myLong.byteValue(); }
Stream API 批量处理
List<Long> data = ...; double avg = data.stream().mapToDouble(Long::doubleValue).average().orElse(0.0);
八、性能优化
避免重复调用
// ❌ 多次调用 if (num.intValue() > 0) { ... } process(num.intValue()); // ✅ 缓存一次 int val = num.intValue(); if (val > 0) { ... } process(val);
优先使用基本类型计算
// ❌ 包装类频繁拆箱 Long sum = 0L; for (Long item : list) { sum += item; // 每次 += 都触发拆箱/装箱 } // ✅ 使用基本类型 long sum = 0L; for (Long item : list) { sum += item; // item 自动拆箱一次 }
避免在循环中频繁转换类型
// ❌ for (int i = 0; i < list.size(); i++) { float f = list.get(i).floatValue(); // 每次调用 } // ✅ 提前转换或缓存 float[] floats = list.stream().mapToFloat(Long::floatValue).toArray();
doubleValue()
vsfloatValue()
性能doubleValue()
通常比floatValue()
稍快(JVM 内部优化更好)- 但
float
占用内存少,适合大数据量存储
九、总结
方法 | 安全性 | 精度 | 用途 |
---|---|---|---|
longValue() |
✅ 安全 | ✅ 无损 | 推荐首选,尤其大整数 |
intValue() |
⚠️ 溢出风险 | ✅ 整数 | 小于 21 亿的数 |
shortValue() |
❌ 高风险 | ✅ 整数 | 特定协议/位操作 |
byteValue() |
❌ 极高风险 | ✅ 整数 | 二进制处理、加密 |
floatValue() |
✅ 安全 | ⚠️ 精度丢失 | 单精度浮点计算 |
doubleValue() |
✅ 安全 | ⚠️ 大数轻微损失 | 推荐浮点转换 |
核心要点:
longValue()
是最安全、最常用的转换方法。- 其他转换必须警惕溢出与精度丢失。
- 自动拆箱简化代码,但不可替代逻辑判断。
null
值必须提前检查,避免NullPointerException
。- 性能关键场景应减少装箱/拆箱和重复转换。