Integer.parseInt()和Integer.valueOf()方法详解
2024-08-27 21:37:39
1.Integer.parseInt():
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
public static int parseInt(String s, int radix) throws NumberFormatException{
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
if (s == null) {
throw new NumberFormatException("null");
} if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +" less than Character.MIN_RADIX");
} if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +" greater than Character.MAX_RADIX");
} int result = 0;
boolean negative = false;
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit; if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s); if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix); //传入一个字符参数和基数,返回一个int值
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
2.Integer.valueOf():
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);
}
最新文章
- GIT 操作
- Android Paint类方法说明
- 这个算asp.net的一个bug吗?
- Redis应用案例,查找某个值的范围(转)
- ios之点语法
- aix i节点
- Android 真机调试显示offline
- 在.csproj文件中增加npm, bower, gulp, webpack的构建
- AI佳作解读系列(二)——目标检测AI算法集杂谈:R-CNN,faster R-CNN,yolo,SSD,yoloV2,yoloV3
- 纯CSS画的基本图形(圆形、三角形、多边形、爱心、八卦等)
- JavaScript深入之从原型到原型链
- Linux下的at定时执行任务命令详解
- margin和padding的用法与区别--以及bug处理方式
- Web设计中打开新页面或页面跳转的方法
- dojo小代码
- 【HTML打印】HTML直接调用window下的打印机并执行打印任务(简单打印任务生成)
- airflow 实战
- iconfont 入门级使用方法
- [JSOI2016]扭动的回文串
- QT源码解析(七)Qt创建窗体的过程,作者“ tingsking18 ”(真正的创建QPushButton是在show()方法中,show()方法又调用了setVisible方法)
热门文章
- Spring velocity 中文乱码 解决方案
- mosquitto $SYS下topic
- 详解 boost 库智能指针(scoped_ptr<;T>; 、shared_ptr<;T>; 、weak_ptr<;T>; 源码分析)
- 关闭危害的端口DOS命令(转载)
- Android 依赖注入: Dagger 2 实例解说(一)
- 跑酷游戏的一些bug总结(滥用FixedUpdate的坑)
- linux 下面压缩,解压.rar文件以及rar,unrar实例
- DLL编写中extern “C”和__stdcall的作用
- 亿级日PV的魅族云同步的核心协议与架构实践(转)
- [转]Python中函数的值传递和引用传递