首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >sonar编码规范之Use "Integer.parseInt" for this string-to-int conversion

sonar编码规范之Use "Integer.parseInt" for this string-to-int conversion

作者头像
johnhuster的分享
发布2022-03-28 14:12:24
发布2022-03-28 14:12:24
4150
举报
文章被收录于专栏:johnhusterjohnhuster

在将字符串转换为整形时sonar推荐的是Integer.parseInt,而不是Integer.valueOf,原因是前者效率更高。进入Integer.valueOf内部就知道了:

代码语言:javascript
复制
    public static Integer valueOf(String s) throws NumberFormatException {
         return Integer.valueOf(parseInt(s, 10));
     }
 
    public static Integer valueOf(int i) {
         assert IntegerCache.high >= 127;
         if (i >= IntegerCache.low && i <= IntegerCache.high)
             return IntegerCache.cache[i + (-IntegerCache.low)];
         return new Integer(i);
     }
 

后者会额外创建一个临时Integer对象,尤其在循环中慎用Integer.valueOf,优先选择Integer.parseInt

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016/12/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档