【findBugs】findBugs对Integer.valueOf()的报错一类不必要的装箱转换

findBugs工具警告的,一类不必要的装箱转换:

“Boxing/unboxing to parse a primitive”, A boxed primitive is created from a String, just to extract the unboxed primitive value. It is more efficient to just call the static parseXXX method

“用于基本类型的装箱/拆箱”,把String解析成基本类型的包装类,如果只是为了获得String的数值大小。调用静态parseXXX方法更有效


例子:

int projectNumber = Integer.valueOf(strNumber) 

Integer.valueOf返回一个Integer,而不是一个int,但是projectNumber变量期望获得一个int值。Findbugs警告你,你正在做一个“漫长”的装箱拆箱操作,涉及潜在地创建一个Integer实例,多余的对象,然后通过传递这个Integer对象再拆箱获得值,例如:

String => int => Integer => int
         ----------------
                \ ---这是在Integer.valueOf中

既然是想得到值,直接从String -> int就好了。

String => int
--------------
      \ --- Integer.parseInt

修改后的代码:int projectNumber = Integer.parseInt(strNumber) 

解决的问题

没有必要进行临时Integer和潜在的内存分配。
如果是Integer projectNumber = Integer.valueOf(strNumber),就没有警告,因为这Integer对象正是你想传递给projectNumber实例。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值