Java基础——`Long` 和 `Integer` 类型之间的转换方法总结

在 Java 中,Long 和 Integer 类型之间的转换是常见的需求。由于 Long 的范围比 Integer 大,所以转换时需要注意潜在的数据丢失问题。以下是将 Long 转换为 Integer 和将 Integer 转换为 Long 的常见方法:

1. Long 转换为 Integer

使用强制类型转换

将 Long 转换为 Integer 可以通过强制类型转换完成。但请注意,这种方法没有范围检查,可能会导致数据丢失或溢出。

java 复制代码
Long longValue = 123456789L;
Integer intValue = longValue.intValue();
使用 Math.toIntExact()

Math.toIntExact() 是 Java 8 引入的安全转换方法,它会在 Long 值超出 Integer 范围时抛出 ArithmeticException 异常。

java 复制代码
Long longValue = 123456789L;
try {
    Integer intValue = Math.toIntExact(longValue);
    System.out.println("Integer value: " + intValue);
} catch (ArithmeticException e) {
    System.out.println("Long value out of range for Integer");
}

2. Integer 转换为 Long

使用 Long 构造函数

将 Integer 转换为 Long 可以使用 Long 构造函数,或者使用 Long.valueOf() 方法。

java 复制代码
Integer intValue = 12345;
Long longValue = Long.valueOf(intValue);
使用自动装箱

由于 Java 的自动装箱(autoboxing),你可以直接将 Integer 赋值给 Long 变量,Java 会自动转换。

java 复制代码
Integer intValue = 12345;
Long longValue = intValue.longValue();

代码示例

java 复制代码
public class Main {
    public static void main(String[] args) {
        // Long to Integer
        Long longValue = 123456789L;
        
        // Method 1: Using intValue() method
        Integer intValue1 = longValue.intValue();
        System.out.println("Long to Integer (intValue()): " + intValue1);
        
        // Method 2: Using Math.toIntExact() method
        try {
            Integer intValue2 = Math.toIntExact(longValue);
            System.out.println("Long to Integer (Math.toIntExact()): " + intValue2);
        } catch (ArithmeticException e) {
            System.out.println("Long value out of range for Integer");
        }
        
        // Integer to Long
        Integer intValue = 12345;
        
        // Method 1: Using Long constructor
        Long longValue1 = Long.valueOf(intValue);
        System.out.println("Integer to Long (Long.valueOf()): " + longValue1);
        
        // Method 2: Using longValue() method
        Long longValue2 = intValue.longValue();
        System.out.println("Integer to Long (longValue()): " + longValue2);
    }
}

总结

  • Long 转 Integer:

    • 使用 intValue() 方法:简单但没有范围检查。
    • 使用 Math.toIntExact():安全,能捕捉超出范围的情况。
  • Integer 转 Long:

    • 使用 Long.valueOf() 或 longValue() 方法:简单且有效。

在进行这些转换时,务必考虑值范围,以避免数据丢失或溢出。

相关推荐
不得了zz7 分钟前
04-Spring AI Alibaba ReactAgent 生产级实践:工具调用、结构化输出、HITL 与上下文管理
java·ai编程·springaialibaba
猿究院_夏燊18 分钟前
SpringMVC 到底是怎么干活的?
java·网络·职场和发展
鶴哥只手遮天19 分钟前
Spring Framework 7.0.9 深度剖析-05-配置元数据解析:XML、注解与 JavaConfig
java·后端·开源
NGINX开源社区27 分钟前
与我们交流:NGINX Gateway Fabric 与 NGINX Ingress Controller 社区会议
java·服务器·前端
鶴哥只手遮天30 分钟前
Spring Framework 7.0.9 深度剖析-16-异步与调度
java·后端·开源
十年Java程序媛37 分钟前
Java 多线程基础与锁机制|进程线程、synchronized、volatile、Lock 面试全梳理
java·spring boot·面试
弈栈录38 分钟前
大模型服务抽象:兼容 OpenAI、通义千问与本地模型
java·后端·架构
鶴哥只手遮天1 小时前
Spring Framework 7.0.9 深度剖析-06-Bean 生命周期与依赖注入原理
java·后端·开源
唐钰小宝B1 小时前
深度解读 JDK 8、JDK 11、JDK 17 和 JDK 21 的区别
java·开发语言·windows
golang学习记1 小时前
最新版VS Code开发Spring Boot项目保姆级教程
java·spring boot·vscode·后端