Integer类源码浅析
1、首先Integer提供了两类工具类,包括把一个int类型转成二进等,

其实执行转换算法只有一个方法:
public static String toString(int i, int radix) {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
/* Use the faster version */
if (radix == 10) {
return toString(i);
}
char buf[] = new char[33];
boolean negative = (i < 0);
int charPos = 32;
if (!negative) {
i = -i;
}
while (i <= -radix) {
buf[charPos--] = digits[-(i % radix)];
i = i / radix;
}
buf[charPos] = digits[-i];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (33 - charPos));
}
2、测试的示例代码
//定义一个Integer 变量
Integer i = 1;
Integer j = 1;
System.out.println(i==j); //true Integer x = 128;
Integer y = 128;
System.out.println(x==y); //false
为什么会出现这样的结果呢,因为Integer内部维护了一个缓存类IntegerCache,默认缓存-128~127的数据
/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the -XX:AutoBoxCacheMax=<size> option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/
IntegerCache缓存类的大小是可以设置,通过 -XX:AutoBoxCacheMax=<size> 这个选项来设置
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
3、Integer类的内部维护了一个int基本类型的变量
提供了两个构造方法来初始化,一个构造方法接受int类型,一个接受String类型,接受类型的构造方法,调用了静态方法parseInt(s,10);
4、还有JDK1.7新添加的一个方法
/**
* Compares two {@code int} values numerically.
* The value returned is identical to what would be returned by:
* <pre>
* Integer.valueOf(x).compareTo(Integer.valueOf(y))
* </pre>
*
* @param x the first {@code int} to compare
* @param y the second {@code int} to compare
* @return the value {@code 0} if {@code x == y};
* a value less than {@code 0} if {@code x < y}; and
* a value greater than {@code 0} if {@code x > y}
* @since 1.7
*/
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
其他方法就不做分析,可以直接参看API
Integer类源码浅析的更多相关文章
- Jdk1.8 之 Integer类源码浅析
先看一下它的继承.实现关系: public final class Integer extends Number implements Comparable<Integer> Number ...
- Long类源码浅析
1.Long类和Integer相类似,都是基本类型的包装类,类中的方法大部分都是类似的: 关于Integer类的浅析可以参看:Integer类源码浅析 2.这里主要介绍一下LongCache类,该缓存 ...
- [原创]Android系统中常用JAVA类源码浅析之HashMap
由于是浅析,所以我只分析常用的接口,注意是Android系统中的JAVA类,可能和JDK的源码有区别. 首先从构造函数开始, /** * Min capacity (other than zero) ...
- ArrayList类源码浅析(一)
1.首先来看一下ArrayList类中的字段 可以看出,ArrayList维护了一个Object数组,默认容量是10,size记录数组的长度: 2.ArrayList提供了三个构造器:ArrayLis ...
- java.lang.Byte 类源码浅析
Byte 类字节,属于Number. public final class Byte extends Number implements Comparable<Byte> { /** * ...
- LinkedList类源码浅析(一)
1.先来看一看LinkedList类的字段和构造方法 size记录链表的长度,first永远指向链表的第一个元素,last永远指向链表的最后一个元素 提供两个构造方法,一个无参的构造方法,一个接受一个 ...
- ArrayList类源码浅析(三)
1.看一个示例 运行上述代码,抛出一个异常: 这是一个典型的并发修改异常,如果把上述代码中的125行注释,把126行打开,运行就能通过了: 原因: 1)因为在迭代的时候,使用的是Itr类的对象,在调用 ...
- ArrayList类源码浅析(二)
1.removeAll(Collection<?> c)和retainAll(Collection<?> c)方法 第一个是从list中删除指定的匹配的集合元素,第二个方法是用 ...
- LinkedList类源码浅析(二)
1.上一节介绍了LinkedList的几个基本的方法,其他方法类似,就不一一介绍: 现在再来看一个删除的方法:remove(Object o) remove方法接受一个Object参数,这里需要对参数 ...
随机推荐
- python——列表方法
L.append():追加一个元素到列表末尾 L = [] L.append('boy') L.insert() :将一个元素插入到指定位置 L.insert(1, 'girl') L.extend( ...
- Jade学习(四)之结合node如何编译执行
1.首先安装node 2.新建一个文件夹并进入该文件夹 3.安装jade 4.在新建的文件夹下新建js文件,写nodejs代码 5.在vscode中利用插件code runner直接执行js文件,输出 ...
- Jdbc Driver驱动和ServerTimeZone时区的的问题
一.JDBC驱动的版本号以及名称问题 区别: com.mysql.jdbc.Driver 是 mysql-connector-java 5中的 com.mysql.cj.jdbc.Driver 是 m ...
- Python Requests库 Get和Post的区别和Http常见状态码
(1) 在客户端,Get方式在通过URL提交数据,数据在URL中可以看到:POST方式,数据放置在HTML HEADER内提交. (2) GET方式提交的数据最多只能有1024 Byte,而P ...
- (转) windows 下ORA-12514:TNS 监听问题
在使用Orcale数据库的时候不知道各位是否遇到过如图的监听问题(或者显示类似的问题),以下方法就是来解决这样的问题的. 首先右击计算机,选择管理.选择左侧栏的服务与应用程序,右侧栏选服务. ...
- (转) ESB 企业服务总线基本内容概述
ESB全称为Enterprise Service Bus,即企业服务总线. 它是传统中间件技术与XML.Web服务等技术结合的产物(SOAP协议= HTTP协议+ XML数据格式). ESB提供了网络 ...
- 利用zookeeper部署kafka集群
1.准备工作: iptables -F #关闭防火墙 systemctl stop firewalld.service #关闭防火墙 准备三台虚拟机并放入/etc/hosts下 192.16 ...
- git 分支 版本回退
本地回退: git reset --hard HEAD~ 回退后推送到远程: git push --force .
- UI控件Telerik UI for WPF发布R2 2019|附下载
Telerik UI for WPF拥有超过100个控件来创建美观.高性能的桌面应用程序,同时还能快速构建企业级办公WPF应用程序.UI for WPF支持MVVM.触摸等,创建的应用程序可靠且结构良 ...
- rsyncd.conf配置文件
rsyncd.conf配置文件 #Rsync server#created by oldboy 15:01 2009-6-5##rsyncd.conf start##uid = rootgid ...