先看一下它的继承、实现关系:
public final class Integer extends Number implements Comparable<Integer>

Number是个抽象类,大概包含六个抽象方法,都是用来类型转换的 具体代码如下:
public abstract class Number implements java.io.Serializable {

public abstract int intValue();

public abstract long longValue();

public abstract float floatValue();

public abstract double doubleValue();

public byte byteValue() {
return (byte)intValue();
}

public short shortValue() {
return (short)intValue();
}

/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -8742448824652078965L;
}
        这就是继承后 子类的实现,强转。
public long longValue(http://www.amjmh.com) {
return (long)value;
}
        看下Comparable 接口,只有一个compareTo 比较大小方法:
public int compareTo(T o);    
            看下Integer的具体        
 public int compareTo(Integer anotherInteger) {
        return compare(this.value, anotherInteger.value);
    }

public static int compare(int x, int y) {
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
    }
      如果看代码有点费劲,那复制如下代码,自己运行下看结果就懂了。
        
public class Test {
public static void main(String[] args) {
Integer it1=128;
Integer it2=-128;
Integer it3=128;
System.out.println(it2.compareTo(it1)+"-----x < y");
System.out.println(it1.compareTo(it2)+"------x > y");
System.out.println(Integer.compare(it1,it3)+"-----x == y");

}
}

打印结果:
-1-----x < y
1------x > y
0-----x == y

2.   Integer的缓存问题

Integer默认放入缓存的区间是-128 至127 。但最大緩存值可以手動調整,請看:

The Javadoc comment clearly states that this class is for cache and to support the autoboxing of values between 128 and 127. The high value of 127 can be modified by using a VM argument -XX:AutoBoxCacheMax=size. So the caching happens in the for-loop. It just runs from the low to high and creates as many Integer instances and stores in an Integer array named cache. As simple as that. This caching is doing at the first usage of the Integer class. Henceforth, these cached instances are used instead of creating a new instance (during autoboxing).

運行以下代碼看效果:
        
public class IntegerTest {
public static void main(String[] args) {
Integer it1=2008;
Integer it2=2008;
System.out.println(it1==it2);//true 说明:为啥超了127了还是true呢?因为我设置了vm arguments参数 -XX:AutoBoxCacheMax=2008
Integer it1t=2009;
Integer it2t=2009;
System.out.println(it1t==it2t);//false 说明:超过 2008 false了吧~~
Integer newit1=new Integer(127);
Integer newit2=new Integer(127);
System.out.println(newit1==newit2);//false 说明:New的话是在heap上占用独立内存的新对象
int it3=127;
int it4=127;
int it5=2008;
System.out.println(it3==it4);//true
System.out.println(it1==it5);//true
}
}
            print result :
        
true
false
false
true
true

設置虛擬機啓動參數
---------------------

Jdk1.8 之 Integer类源码浅析的更多相关文章

  1. Integer类源码浅析

    1.首先Integer提供了两类工具类,包括把一个int类型转成二进等, 其实执行转换算法只有一个方法: public static String toString(int i, int radix) ...

  2. Long类源码浅析

    1.Long类和Integer相类似,都是基本类型的包装类,类中的方法大部分都是类似的: 关于Integer类的浅析可以参看:Integer类源码浅析 2.这里主要介绍一下LongCache类,该缓存 ...

  3. [原创]Android系统中常用JAVA类源码浅析之HashMap

    由于是浅析,所以我只分析常用的接口,注意是Android系统中的JAVA类,可能和JDK的源码有区别. 首先从构造函数开始, /** * Min capacity (other than zero) ...

  4. ArrayList类源码浅析(一)

    1.首先来看一下ArrayList类中的字段 可以看出,ArrayList维护了一个Object数组,默认容量是10,size记录数组的长度: 2.ArrayList提供了三个构造器:ArrayLis ...

  5. java.lang.Byte 类源码浅析

    Byte 类字节,属于Number. public final class Byte extends Number implements Comparable<Byte> { /** * ...

  6. LinkedList类源码浅析(一)

    1.先来看一看LinkedList类的字段和构造方法 size记录链表的长度,first永远指向链表的第一个元素,last永远指向链表的最后一个元素 提供两个构造方法,一个无参的构造方法,一个接受一个 ...

  7. ArrayList类源码浅析(三)

    1.看一个示例 运行上述代码,抛出一个异常: 这是一个典型的并发修改异常,如果把上述代码中的125行注释,把126行打开,运行就能通过了: 原因: 1)因为在迭代的时候,使用的是Itr类的对象,在调用 ...

  8. ArrayList类源码浅析(二)

    1.removeAll(Collection<?> c)和retainAll(Collection<?> c)方法 第一个是从list中删除指定的匹配的集合元素,第二个方法是用 ...

  9. LinkedList类源码浅析(二)

    1.上一节介绍了LinkedList的几个基本的方法,其他方法类似,就不一一介绍: 现在再来看一个删除的方法:remove(Object o) remove方法接受一个Object参数,这里需要对参数 ...

随机推荐

  1. 常见的网络设备:集线器 hub、网桥、交换机 switch、路由器 router、网关 gateway

    Repeater 中继器 Hub 集线器 bridge 网桥 switch 交换机 router 路由器 gateway 网关 网卡 参考资料: do-you-know-the-differences ...

  2. 【ABAP系列】SAP DOI技术中I_OI_SPREADSHEET接口的使用

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP DOI技术中I_OI_S ...

  3. poj2376Cleaning Shifts (贪心求解)

    描述 大表哥分配 N (1 <= N <= 25,000) 只中的一些奶牛在牛棚附近做些清洁. 他总是要让至少一只牛做清洁.他把一天分成T段(1 <= T <= 1,000,0 ...

  4. hihoCoder 1014 : Trie树(字典树)

    传送门 Description 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这一天,他们遇到了一本词典,于是小Hi就向小 ...

  5. Linux下复杂PC问题——多进程编程/信号量通信/共享存储区

    进程相关函数 pid_t fork(); 头文件:unistd.h,sys/types.h 作用:建立一个新进程(子进程),子进程与原进程(父进程)共享代码段,并拥有父进程的其他资源(数据.堆栈等)的 ...

  6. IDEA 光标显示注释

  7. smarty中判断数组是否为空的方法

    1,用count来取得数组的下标个数 下面例子中,如果$array为空则不输出任何数据 以下为引用的内容:{if $array|@count neq 0 }... ...{/if} 2,直接来判断 以 ...

  8. 攻防世界--crackme

    测试文件:https://adworld.xctf.org.cn/media/task/attachments/088c3bd10de44fa988a3601dc5585da8.exe 1.准备 获取 ...

  9. C中char类型的用法

    代码 /* char类型的用法 */ #include <stdio.h> main(int argc, char *argv[]) { /* 声明字符变量c1 */ char c1 = ...

  10. Nginx学习总结:geo与image模块(四

    斜体下划线,表示建议采用默认配置,无需显式的配置 一.ngx_http_geo_module 核心特性为:根据客户端IP(段),geo模块将会匹配出指定的变量(比如,国家代码,城市代码).geo模块可 ...