Integer.parseInt("") Integer.valueOf("")和new Integer("")之间的区别

简介: 把一个String转换成int有Integer.parseInt("")、 Integer.valueOf("")和new Integer("")这么几种方式,它们之间有什么区别呢?我们可以分别看一下它们的源码//Integer.parseInt("")public static int parseInt(String s) throws NumberFormatExc

把一个String转换成int有Integer.parseInt("")Integer.valueOf("")new Integer("")这么几种方式,它们之间有什么区别呢?我们可以分别看一下它们的源码

//Integer.parseInt("")
public static int parseInt(String s) throws NumberFormatException {
  return parseInt(s,10);
}
//Integer.valueOf("")
public static Integer valueOf(String s) throws NumberFormatException {
  return Integer.valueOf(parseInt(s, 10));
}
//new Integer("")
public Integer(String s) throws NumberFormatException {
  this.value = parseInt(s, 10);
}

从源码中可以看出,Integer.valueOf("")Integer.parseInt("")内部实现是一样的,它们之间唯一的区别就是Integer.valueOf(“”)返回的是一个Integer对象,而Integer.parseInt(“”)返回的是一个基本类型的int

我们再看Integer.valueOf("")new Integer(""),它们同样返回的是一个Integer对象,但它们又有什么区别呢?我们再进入Integer.valueOf(parseInt(s, 10) )方法内部:

public static Integer valueOf(int i) {
  if (i >= IntegerCache.low && i <= IntegerCache.high)
  return IntegerCache.cache[i + (-IntegerCache.low)];
  return new Integer(i);
}

我们可以看到,Integer.valueOf("")会用到IntegerCache对象,当IntegerCache中存在时就从cache中取,不存在时才会调用new Integer(i)构造函数返回一个Integer对象。所以Integer.valueOf("")会用到cache,其效率可能会比用构造函数new Integer(i)

关于IntegerCache,在-127~128之间的值都会被cache,所以当我们要的值位于这个区间时返回的都是同一个实例,例如:

System.out.println(Integer.valueOf(5) == Integer.valueOf(5));
System.out.println(Integer.valueOf(500) == Integer.valueOf(500));

上面程序的运行结果是:

    false //会用到缓存
    true  //不会用到缓存

综上所述:

  • 当我们需要的是一个基本类型* int *的时候我们需要使用Integer.parseInt()函数
  • 当我们需要的是一个Integer对象类的时候我们就是用Integer.valueOf()函数
目录
相关文章
|
8月前
|
JavaScript 编译器
模块 &quot;&quot;element-plus&quot;&quot; 没有导出的成员 &quot;ElMessage&quot;。你是想改用 &quot;import ElMessage from &quot;element-plus&quot;&quot; 吗?
模块 &quot;&quot;element-plus&quot;&quot; 没有导出的成员 &quot;ElMessage&quot;。你是想改用 &quot;import ElMessage from &quot;element-plus&quot;&quot; 吗?
225 0
|
JSON fastjson 数据格式
报错:JSONException: illegal identifier : {pos 1, line 1, column 2{{&quot;group&quot;:&quot;trade_p0&qu
报错:JSONException: illegal identifier : {pos 1, line 1, column 2{{&quot;group&quot;:&quot;trade_p0&qu
459 0
|
Java
Can&#39;t convert boolean to string automatically, because the &quot;boolean_format&quot; setting was &quot;true,false&quot;
五月 11, 2017 5:06:50 下午 freemarker.log._JULLoggerFactory$JULLogger error 严重: Error executing FreeMarker template FreeMarker template error: Can't con...
2727 0
|
自然语言处理
合同结构化文书解析失败,请联系管理员排查:{&quot;code&quot;:3001,&quot;message&quot;:&quot;File transform error&quot;,&quot;success&quot;:false,&quot;tracerId&quot;:&quot;requestId&quot;}报错处理
在使用自然语言处理自学习平台时,标注任务需要上传标注数据,但是使用doc格式上传文件后开始标注时出现了此提示,此篇文章简单介绍下此问题的处理方式。
609 0
合同结构化文书解析失败,请联系管理员排查:{&quot;code&quot;:3001,&quot;message&quot;:&quot;File transform error&quot;,&quot;success&quot;:false,&quot;tracerId&quot;:&quot;requestId&quot;}报错处理
|
Web App开发 Java Apache
java.lang.NumberFormatException: For input string: &quot;undefined&quot;
在将字符串转换为数字时导致此错误,解决此问题的思路:1、添加Try catch语句,2、判断字符串是否为数字,将介绍java中判断字符串是否为数字的方法的几种方法。 完整错误信息: java.
2057 0
|
JavaScript 前端开发 Java
var oButtonView1 = oView.byId(&quot;ButtonView1&quot;);
var oButtonView1 = oView.byId(&quot;ButtonView1&quot;);
var oButtonView1 = oView.byId(&quot;ButtonView1&quot;);
1132. Cut Integer (20) 除零
Cutting an integer means to cut a K digits long integer Z into two integers of (K/2) digits long integers A and B.
1084 0

热门文章

最新文章