
一、比较两个 Integer 对象在 Java 中比较两个 Integer 对象可能为 null时可以使用 Objects 的 equals 方法// 判断 a 与 b 是否相等包括都为 nullIntegera10;Integerbnull;booleanresultObjects.equals(a,b);System.out.println(result);二、Integer 转 Long1、具体实现方式 1推荐Integeri100;Longli.longValue();返回 int 转换后的 long 然后通过自动装箱将 long 变成 Long 对象方式 2Integeri100;LonglLong.valueOf(i);Long.valueOf(long) 接收 long这里 i 是 Integer 对象 先进行拆箱i.intValue() 得到 int然后转为 long 再调用 Long.valueOf(long)2、注意事项不能直接强转Integeri100;Longl(Long)i;# 输出结果 错误: 不兼容的类型: Integer无法转换为Long注意空值Integerinull;Longlinull?null:i.longValue();三、Long 转 Integer1、具体实现方式 1推荐Longl100L;Integeril.intValue();方式 2Longl100L;Integeri(int)l.longValue();2、注意事项注意溢出风险// 超出 int 范围Longl2147483648L;Integeril.intValue();System.out.println(i);# 输出结果 -2147483648// 安全处理Longl2147483648L;if(lInteger.MAX_VALUE||lInteger.MIN_VALUE){thrownewArithmeticException(Long 值超出 int 范围);}Integeril.intValue();# 输出结果 java.lang.ArithmeticException: Long 值超出 int 范围