Integer.getInteger()与Integer.parseInt()的区别
2015-12-03 14:50
330 查看
Integer.parseInt()
Parses the specified string as a signed decimal integer value. The ASCII character \u002d ('-') is recognized as the minus sign.
源码如下
2.Integer.getInteger()
Returns the
返回的是系统常量的int值
其源码是这样的
public static Integer getInteger(String string) {
if (string == null || string.length() == 0) {
return null;
}
String prop = System.getProperty(string);
if (prop == null) {
return null;
}
try {
return decode(prop);
} catch (NumberFormatException ex) {
return null;
}
}
所以,不要望文生义,用了后面这方法
Parses the specified string as a signed decimal integer value. The ASCII character \u002d ('-') is recognized as the minus sign.
源码如下
public static int parseInt(String string, int radix) throws NumberFormatException { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException("Invalid radix: " + radix); } if (string == null) { throw invalidInt(string); } int length = string.length(), i = 0; if (length == 0) { throw invalidInt(string); } boolean negative = string.charAt(i) == '-'; if (negative && ++i == length) { throw invalidInt(string); } return parse(string, i, radix, negative); } private static int parse(String string, int offset, int radix, boolean negative) throws NumberFormatException { int max = Integer.MIN_VALUE / radix; int result = 0, length = string.length(); while (offset < length) { int digit = Character.digit(string.charAt(offset++), radix); if (digit == -1) { throw invalidInt(string); } if (max > result) { throw invalidInt(string); } int next = result * radix - digit; if (next > result) { throw invalidInt(string); } result = next; } if (!negative) { result = -result; if (result < 0) { throw invalidInt(string); } } return result; }没有太明白里面radix的作用。
2.Integer.getInteger()
Returns the
Integervalue of the system property identified by
string. Returns
nullif
stringis
nullor empty, if the property can not be found or if its value can not be parsed as an integer.
返回的是系统常量的int值
其源码是这样的
public static Integer getInteger(String string) {
if (string == null || string.length() == 0) {
return null;
}
String prop = System.getProperty(string);
if (prop == null) {
return null;
}
try {
return decode(prop);
} catch (NumberFormatException ex) {
return null;
}
}
所以,不要望文生义,用了后面这方法
相关文章推荐
- smarty框架中 点击即可编辑标题的 jquery实现代码
- 一个form表单包含多个submit提交动作
- Mattt大神写单例用的代码片段
- Android Layouts
- 实用教程:如何着色更高效
- 主流编程语言简介
- Android获取通讯录中的所有联系人工具类
- 设备管理器库 libudev
- Log4j配置很详细
- Object-c字符串与数组
- 关于优先级反转【转】
- UVA 11922 Permutation Transformer(Splay Tree)
- 传统企业信息化
- 圆角Panel
- c#连接mysql
- Java中判断字符串是否为数字的五种方法
- Moodle 平台中如何批量设置选择题选项 随机排列
- Dnsmasq安装与配置
- acm新手 偶数求和做法。。。
- Android中弹出PopupWindow让屏幕变暗