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

最新文章

  1. 疑问,关于win64无法使用debug的问题
  2. 2015-12-23-( dispaly:table的用法)
  3. OpenStack Mitaka安装
  4. python单线程爬虫code
  5. java系列-安装MySql(三)
  6. machine learning-----&gt;学习成绩
  7. 一个高度压缩的bit位图字典的实现
  8. Java基础之线程——管理线程同步方法(BankOperation2)
  9. [笔记]--在Windows下配置Git
  10. Oracle数据库程序包全局变量的应用
  11. 常用WebService收集
  12. 关于微信浏览不能URL传参,URL中的问号被删除
  13. Java程序猿的JavaScript学习笔记(8——jQuery选择器)
  14. oracle 监控执行的sql语句
  15. hadoop 读取文件的两种方式
  16. POJ 1523 SPF (去掉割点能形成联通块的个数)
  17. Unity3D中如何计算场景中的三角面和顶点数
  18. 一起学Android之Intent
  19. 痞子衡嵌入式:开源软件协议(MIT/BSD/Apache/LGPL/MPL/GPL)
  20. Servlet(五):一个Servlet处理多个请求

热门文章

  1. C++中的三种继承方式
  2. C++中前置操作符和后置操作符的重载
  3. SSH自动登录config文件配置
  4. grunt接触
  5. 关于css中touch-action属性 在移动端开发中遇到的问题
  6. CentOS 基础命令
  7. 火车采集用到的access查询命令小结
  8. Django学习系列4:编写第一个简单的应用代码
  9. 【洛谷P2602】数字计数
  10. 第六篇:Scrapy框架