1. 方法定义与功能

方法 返回类型 功能说明
long longValue() long 返回 Long 对象的 long 值(无精度损失)
int intValue() int long 值强制转换为 int(可能丢失高32位)
byte byteValue() byte long 值强制转换为 byte(截取低8位)
short shortValue() short long 值强制转换为 short(截取低16位)
float floatValue() float long 值转换为 float(可能损失精度)
double doubleValue() double long 值转换为 double(无精度损失,因 double 范围更大)

2. 示例代码

public class LongConversionExample {
    public static void main(String[] args) {
        Long num = 1234567890123L;  // 13位数字

        // 1. 无精度损失转换
        long lVal = num.longValue();  // 1234567890123L
        double dVal = num.doubleValue(); // 1234567890123.0

        // 2. 可能丢失精度的转换
        int iVal = num.intValue();     // 截取低32位: -539222987
        short sVal = num.shortValue(); // 截取低16位: 7227
        byte bVal = num.byteValue();   // 截取低8位:  -21
        float fVal = num.floatValue(); // 1.23456794E12 (精度损失)
        
        System.out.println("long: " + lVal);
        System.out.println("double: " + dVal);
        System.out.println("int: " + iVal);
        System.out.println("short: " + sVal);
        System.out.println("byte: " + bVal);
        System.out.println("float: " + fVal);
    }
}

输出

long: 1234567890123
double: 1.234567890123E12
int: -539222987
short: 7227
byte: -21
float: 1.23456794E12

3. 关键特性与注意事项

方法 关键特性 注意事项
longValue 无损转换 最安全的转换方式
intValue 截断高32位 大数转换会导致结果错误(如 0x12345678ABCDEL0x5678ABCDE
byteValue 截断高56位,仅保留最低8位 结果范围:-128127
shortValue 截断高48位,仅保留最低16位 结果范围:-3276832767
floatValue 可能损失精度(float 仅6-7位有效数字) long > 2²⁴ (约1600万) 时精度丢失(如 123456789123456792.0
doubleValue 无损转换(double 可精确表示所有 long 值) 最佳的大整数浮点数转换方式

4. 常见错误与解决方案

  1. 大数转整型的精度丢失

    Long bigNum = 9_000_000_000_000L;
    int wrong = bigNum.intValue(); // 返回无意义值 (低32位)
    

    解决:添加范围检查

    if (bigNum >= Integer.MIN_VALUE && bigNum <= Integer.MAX_VALUE) {
        int safe = bigNum.intValue();
    } else {
        // 处理溢出
    }
    
  2. 浮点数精度问题

    Long id = 123456789L;
    float f = id.floatValue(); // 实际值: 123456792.0
    

    解决:优先使用 doubleValue() 或保留 long 类型

  3. 自动装箱陷阱

    Object num = 1000L;
    short s = ((Long) num).shortValue(); // 返回 1000 & 0xFFFF = -24 (错误)
    

    解决:明确转换范围

    if (num >= Short.MIN_VALUE && num <= Short.MAX_VALUE) {
        short safe = num.shortValue();
    }
    

5. 最佳实践

  1. 优先选择无损转换

    // 推荐
    long accountId = entity.getId().longValue();
    double scientificValue = bigNumber.doubleValue();
    
    // 避免
    int riskyId = entity.getId().intValue(); // 可能溢出
    
  2. 大数处理使用 BigInteger

    BigInteger hugeNum = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE);
    int safeInt = hugeNum.intValueExact(); // 溢出时抛出异常
    
  3. 数值范围检查工具

    public static int safeLongToInt(long value) {
        if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
            throw new ArithmeticException("整数溢出: " + value);
        }
        return (int) value;
    }
    

6. 性能说明

  • 开销极低:所有方法均为简单位操作或类型转换
  • 对比
    // 直接转换 (高效)
    long l = num.longValue();
    
    // 间接转换 (低效)
    long l = Long.parseLong(num.toString()); // 避免这样用!
    

总结

场景 推荐方法 原因
获取原始长整型 longValue() 零开销,无精度损失
转换为浮点数 doubleValue() 无精度损失,范围更大
小范围整数转换 intValue()/shortValue() 需严格检查范围 (Integer.MIN_VALUE ~ MAX_VALUE)
字节操作 byteValue() 处理二进制数据时使用
避免使用场景 floatValue() 精度损失严重,除非内存限制

核心原则

  • 处理大整数 → 优先用 longValue()doubleValue()
  • 转换到小范围整型 → 必须检查范围
  • 精确计算 → 避免 floatValue()