先看一下它的继承、实现关系:
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

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

最新文章

  1. Decode Ways
  2. 差分约束系统 POJ 3169 Layout
  3. 服务器IP地址后修改SQL Server配置
  4. Samba配置文件常用参数详解-OK
  5. 单点登录CAS使用记(六):单点登出、单点注销
  6. 开源 免费 java CMS - FreeCMS1.9 会员组管理
  7. ssh免密码和ssh-copy-id命令
  8. Spring入门导读——IoC和AOP
  9. mysql 安装-zip版
  10. LDAP客户端在Windows环境的部署及配置
  11. mysql索引规范
  12. nist-sha
  13. 访问mysql出现Access denied for user &#39;root&#39;@&#39;的解决方法
  14. Python3学习笔记20-获取对象信息
  15. C memset
  16. 用Telnet测试服务器的端口是否开通
  17. vue-cli 上手
  18. Spring Cloud学习入门路线方案
  19. angular 2+ 路由守卫
  20. 10g RAC常用开启关闭命令 – SRVCTL用法

热门文章

  1. OAuth 2.0 综述
  2. ALDS1_1_3_D Areas on the Cross-Section Diagram 遇见了几个有意思的语法问题
  3. Step-by-step from Markov Process to Markov Decision Process
  4. openssl使用
  5. python网络编程之粘包
  6. 基于Netty重构RPC框架
  7. maven配置本地仓库、maven配置阿里中央仓库、eclipse配置maven
  8. jvm学习(5) 对象的创建与结构
  9. Panabit镜像功能配合wireshark抓包的方法
  10. sublime text3 安装插件