Integer.parseInt(String s) 和 Integer.valueOf(String s) 的区别
通过查看java.lang.Integer的源码可以发现, 它们最终调用的都是
/**
* Parses the string argument as a signed integer in the radix
* specified by the second argument. The characters in the string
* must all be digits of the specified radix (as determined by
* whether {@link java.lang.Character#digit(char, int)} returns a
* nonnegative value), except that the first character may be an
* ASCII minus sign {@code '-'} (<code>'\u002D'</code>) to
* indicate a negative value or an ASCII plus sign {@code '+'}
* (<code>'\u002B'</code>) to indicate a positive value. The
* resulting integer value is returned.
*
* <p>An exception of type {@code NumberFormatException} is
* thrown if any of the following situations occurs:
* <ul>
* <li>The first argument is {@code null} or is a string of
* length zero.
*
* <li>The radix is either smaller than
* {@link java.lang.Character#MIN_RADIX} or
* larger than {@link java.lang.Character#MAX_RADIX}.
*
* <li>Any character of the string is not a digit of the specified
* radix, except that the first character may be a minus sign
* {@code '-'} (<code>'\u002D'</code>) or plus sign
* {@code '+'} (<code>'\u002B'</code>) provided that the
* string is longer than length 1.
*
* <li>The value represented by the string is not a value of type
* {@code int}.
* </ul>
*
* <p>Examples:
* <blockquote><pre>
* parseInt("0", 10) returns 0
* parseInt("473", 10) returns 473
* parseInt("+42", 10) returns 42
* parseInt("-0", 10) returns 0
* parseInt("-FF", 16) returns -255
* parseInt("1100110", 2) returns 102
* parseInt("2147483647", 10) returns 2147483647
* parseInt("-2147483648", 10) returns -2147483648
* parseInt("2147483648", 10) throws a NumberFormatException
* parseInt("99", 8) throws a NumberFormatException
* parseInt("Kona", 10) throws a NumberFormatException
* parseInt("Kona", 27) returns 411787
* </pre></blockquote>
*
* @param s the {@code String} containing the integer
* representation to be parsed
* @param radix the radix to be used while parsing {@code s}.
* @return the integer represented by the string argument in the
* specified radix.
* @exception NumberFormatException if the {@code String}
* does not contain a parsable {@code int}.
*/
public static int parseInt(String s, int radix)
throws NumberFormatException {
}
这个parseInt是可以将字符串解析为各种进制的整数的, parseInt(String s)只是radix=10时的特例
而Integer.parseInt() 和 Integer.valueOf() 的区别主要在于放回的类型上, 一个是 int, 一个是Integer, 如果需要的是int, 性能上parseInt()会好点
public static Integer valueOf(String s) throws NumberFormatException {
return Integer.valueOf(parseInt(s, 10));
}
这里面调用的是 valueOf(int i) 方法, 其源码中为 -128 ~ 128 之间的Integer对象很贴心地做了缓存以提高效率
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.parseInt(String s) 和 Integer.valueOf(String s) 的区别的更多相关文章
- Java面试必看之Integer.parseInt()与Integer.valueOf()
Integer.parseInt()和Integer.valueOf()都是将成为String转换为Int,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举? 我们来深挖 ...
- 深挖的Java源代码之Integer.parseInt()vs Integer.valueOf()
Integer.parseInt()和Integer.valueOf()都是用来将String转换为Int的,但是为什么Java会提供两个这样的方法呢,他们如果是同样的操作,岂不是多此一举? 我们来深 ...
- Integer.parseInt(s)、Integer.valueOf(s)与new Integer()的异同
我们在开发过程中,很多时候需要将String类型数据转换成Integer,而比较常用的方式就是--nteger.parseInt(s).Integer.valueOf(s)与new Integer() ...
- java 解决 java.lang.Integer cannot be cast to java.lang.String
1.在执行代码打印map的value时,提示错误java.lang.Integer cannot be cast to java.lang.String,这个错误很明显是类型转换错误 查看表字段的数 ...
- dividend = Integer.parseInt(args[0])参数问题
先来一段代码: package yichang; public class MyExceptionTest { public static void main(String[] args) { int ...
- Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么?
Integer.valueof(String s)和Integer.parseInt(String s)的具体区别是什么? Integer.valueof(String s)是将一个包装类是将一个实际 ...
- Integer.valueOf(String) 方法之惑
本文由 ImportNew - 靳禹 翻译自 stackoverflow.欢迎加入翻译小组.转载请见文末要求. 有个仁兄在 StackOverflow 上发起了一个问题,是这么问的: “ 我被下面的代 ...
- [转]Integer.valueOf(String) 方法之惑
具体问题以前偶然遇到过,好象是一个java答题得分的论坛,当时做错还研究了下怎么回事,但是前两天遇到类似问题却没想起来.巩固下基础,转了下面文章. 以下内容转自:http://www.importne ...
- Java面试题之Integer.valueOf(String s);采用了什么设计模式
Integer.valueOf(String s);//采用了亨元设计模式: 亨元模式: 它是以一种“节约内存,提高性能”为出发点的设计模式,运用共享技术有效的支持大量细粒度对象的复用. 源码解析: ...
随机推荐
- git 错误:
git 错误: $ git commit -afatal: Unable to create 'e:/git/Android/XXXXXX/.git/index.lock': File exists ...
- zend studio 9.0.4 破解、汉化和字体颜色及快捷键相关设置
转载:http://www.penglig.com/post-45.html 下载:http://www.geekso.com/component/zendstudio-downloads/ 破解:h ...
- Linux双机信任,适用统一安装
一.生成建立安全信任关系的证书. 在A机root用户下执行ssh-keygen命令,在需要输入的地方,直接回车, # ssh-keygen -t rsa 注:直接回车就行 二.查看生成密钥的文件 # ...
- 我参加了51CTO博客大赛,求投票!
我是张传波,也是Fireball(火球). 我参加了51CTO博客大赛,距离网络投票截止没有几天了,求投票!我的参赛链接:http://blog.51cto.com/contest2013/82313 ...
- Asp.Net MVC 自定义的MVC框架(非EF操作数据库)
一些废话:在北京辞职回家不知不觉中已经半年多了,这半年中有过很多的彷徨,困惑,还有些小小难受.半年时间算是我人生以来遇到过的最困苦的时候.理想的工作跟我擦肩而过,驾照也没有考过,年后这一改革...,毕 ...
- macbook安装win7
通常大家都喜欢购买苹果电脑,因为配置高,速度快,但是却不喜欢使用ios系统,这时候需要在macbook上安装windows系统 全新的macbook进行windows的安装,基本大家都会,使用boot ...
- WPF学习之路(三) 属性与依赖
类型是DependencyProperty的属性是依赖属性 依赖属性不同于普通的.Net属性,类似于一个计算过程,根据依赖的值得到最终值. 为什么引入依赖属性: MSDN原文 One of the p ...
- Java中怎样创建线程安全的方法
面试问题: 下面的方法是否线程安全?怎样让它成为线程安全的方法? class MyCounter { private static int counter = 0; public static int ...
- 因GIT默认忽略.dll文件导致的Visual Studio项目通过Bamboo编译失败
背景 由GIT管理的Visual Studio项目,使用Stash管理远端代码库,通过与Stash集成的Bamboo生成项目并发布 现象 Visual Studio项目本地生成成功,用SourceTr ...
- android 基本布局(RelativeLayout、TableLayout等)使用方法及各种属性
本文介绍 Android 界面开发中最基本的四种布局LinearLayout.RelativeLayout.FrameLayout.TableLayout 的使用方法及这四种布局中常用的属性. ...