JDK Integer
1. public static int parseInt(String s, int radix)
a. 充分考虑各种异常情况:字符串为空,带符号,进制出界,计算值出界
b. 计算时转换为负数进行处理:
Integer.MIN_VALUE直接变换符号会导致数值溢出
Integer.MAX_VALUE = 0x7fffffff (+2147483647)
Integer.MIN_VALUE = 0x80000000 (-2147483648, -10000000000000000000000000000000)
= Integer.MAX_VALUE + 1 = Math.abs(Integer.MIN_VALUE) = -1 * Integer. // 最大整数加1后等于最小整数
public static int abs(int a) { return (a < 0) ? -a : a; } // 注意如果参数等于Integer的最小值,将返回相同的值。
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);
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. static int stringSize(int x)
使用常量穷举,避免除法和求余等计算
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
// Requires positive x
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}
JDK Integer的更多相关文章
- 001-java 设计模式概述
一.概述 思维导图 GoF(“四人帮”,又称Gang of Four,即Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides) 1 ...
- java jdk缓存-128~127的Long与Integer
先推断下以下代码的输出结果 Qa:---------------------------------------------- Long a = Long.valueOf(127) ...
- JDK中Integer类的进制转换实现
JDK中关于Integer类的进制转换方法很精巧,具体实现如下: final static char[] digits = { '0' , '1' , '2' , '3' , '4' , '5' , ...
- Integer.parseInt不同jdk源码解析
执行以下代码: System.out.println(Integer.parseInt("-123")); System.out.println(Integer.parseInt( ...
- JAVA源码之JDK(二)——Integer、Long、Double
这篇文章继续java.lang包下的源码学习,笔者也是找了几个比较常用的来阅读.下面针对Integer.Long.Double这样的基本类型的封装类,记录一些比较经典.常用的方法的学习心得,如toSt ...
- JDK源码学习笔记——Integer
一.类定义 public final class Integer extends Number implements Comparable<Integer> 二.属性 private fi ...
- JDK源码分析 – Integer
Integer类的申明 public final class Integer extends Number implements Comparable<Integer> { … } Int ...
- 【JDK】:java.lang.Integer源码解析
本文对JDK8中的java.lang.Integer包装类的部分数值缓存技术.valueOf().stringSize().toString().getChars().parseInt()等进行简要分 ...
- JDK源码之Integer类分析
一 简介 Integer是int基本类型的包装类,同样继承了Number类,实现了Comparable接口,String类中的一些转化方法就使用了Integer类中的一些API,且fianl修饰不可继 ...
随机推荐
- solr :term 查询, phrase查询, boolean 查询
搜索总体有:term 查询, phrase查询, boolean 查询 1. SOLR搜索覆盖度和准确度保证的三个搜索方式: 保证准确率: AND: Search for two different ...
- 第6章 使用springboot整合netty搭建后台
我们不会去使用自增长的id,在现阶段的互联网开发过程中,自增长的id是已经不适用了.在未来随着系统版本的迭代,用户数量的递增,肯定会做分库分表,去做一些相应的切分.在这个时候我们就需要有一个唯一的id ...
- css中字间距调整(转)
css中字间距调整 (2014-01-02 13:51:07) 标签: 汉字 英文 间距 letter-spacing word-spacing 杂谈 分类: 网页制作 汉字间字间距设置:letter ...
- eclipse格式化代码模板
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <profi ...
- POJ 1741 点分治
方法:指针扫描数组 每次选择树的重心作为树根,从树根出发进行一次DFS,求出点到树根的距离,把节点按照与树根的的距离放进数组d,设置两个指针L,R分别从前.后开始扫描,每次满足条件时答案累加R-L., ...
- JavaScript的self和this使用小结
一.self这个非常简单.我们知道,打开任何一个网页,浏览器会首先创建一个窗口,这个窗口就是一个window对象,也是js运行所依附的全局环境对象和全局作用域对象.self 指窗口本身,它返回的对象跟 ...
- 利用脚本,一键设置java环境变量(默认安装路径)
Windows一键设置Java环境变量 右击以管理员方式运行,注意自行更改JAVA_HOME目录文件安装目录. JDKSetting.bat @echo off color 0a echo.----- ...
- chrome headless
最近才知道有这么个东西,说白了就是chrome浏览器的命令行模式,一说到命令行自然就和自动化 高效率有关系,感觉对于自动化测试和爬虫很有用啊
- Flask框架 之 功能详解
浏览目录 配置文件 路由系统 视图 请求相关 响应 模板渲染 session 闪现 中间件 蓝图(blueprint) 特殊装饰器 配置文件 知识点 给你一个路径 “settings.Foo”,可以找 ...
- Ubuntu12.04不能连接小米开发(转)
发现Ubuntu12.04不能连接小米开发,adb devices不能看到设备! 搞了一个上午才搞成功! 小米手机利用USB连接到Ubuntu 12.04系统.运行下面的命令: longskywan ...