`

Integer的“==”返回值

阅读更多

 

/**
 * Created by spring on 10/26/13.
 */
public class Test {
    public static void main(String args[]){

        Integer i_128 = -128;
        Integer j_128 = -128;

        System.out.println(i_128==j_128);

        Integer i_129 = -129;
        Integer j_129 = -129;

        System.out.println(i_129==j_129);


        Integer i128 = 128;
        Integer j128 = 128;

        System.out.println(i128==j128);

        Integer i127 = -127;
        Integer j127 = -127;

        System.out.println(i127==j127);


    }
}

 输出:

true
false
false
true

 

这个原因想必大家都知道,Integer是一个不可变类,所以对一些常用的值(-128--127)进行了缓存,避免对象的频繁创建和销毁。但是从jdk1.5开始这个缓存的范围可以由开发者根据需要自己指定,不过下限还是-128,上限可以修改。jdk的源码如下:

    /**
     * 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.
     */

    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));
            }
            high = h;

            cache = new Integer[(high - low) + 1];
            int j = low;
            for(int k = 0; k < cache.length; k++)
                cache[k] = new Integer(j++);
        }

 

    /**
     * Returns an {@code Integer} instance representing the specified
     * {@code int} value.  If a new {@code Integer} instance is not
     * required, this method should generally be used in preference to
     * the constructor {@link #Integer(int)}, as this method is likely
     * to yield significantly better space and time performance by
     * caching frequently requested values.
     *
     * This method will always cache values in the range -128 to 127,
     * inclusive, and may cache other values outside of this range.
     *
     * @param  i an {@code int} value.
     * @return an {@code Integer} instance representing {@code i}.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        assert IntegerCache.high >= 127;
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

 

分享到:
评论

相关推荐

    景兴达门禁

    实时处理流程 1、打开网络 function JX102R_open_Net(): integer; 2、绑定端口,这步可选,... 如果返回值为零,说明有刷卡请求 判断后,调用function JX102R_Rsp_Card(ReaderNO, TypeId: integer): integer;应答

    delphi通用函数单元一

    function IntToStrEx(Value: Integer; Len: Integer; FillChar: Char = '0'): string; {测试通过} {* 扩展整数转字符串函数 Example: IntToStrEx(1,5,'0'); 返回:"00001"} function IntToStrSp(Value: Integer; ...

    设置屏幕分辨率小程序

    integer Fblz:Fblz函数名,integer表示有返回值,类型=整型 。 integer x:数值型形参 integer y:数值型形参 三、Vfp调用举例: Declare integer Fblz In "fBL.dll" integer x,integer y x=800 y=600 xx=...

    HugeInteger

    每个函数的返回值为布尔类型。 函数原型为 boolean operation(HugeInteger) 注意: 1. 大数运算需要考虑到正负,使用单独的变量存储符号位。 2. 大数的输入: 正数形式为”12345”,负数形式为”-123456” 3. 如果...

    Director COM 串口 插件 功能实现

    integer failNum 返回值为0,表示初始化成功,否则为失败 示例: xCom.init(1, 9600) 说明: a、在通常的串口通信中,还有数据位、停止位、奇偶校验位、电平控制等设置,为了简化应用,本Xtra默认设置为8位数据位...

    VB测试键盘是被按下Shift键、Ctrl键等功能键.rar

     Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)  'Print Shift  If Shift = 1 Then '判断键盘事件中Shift 参数的返回值  Label1.Caption = "键盘上曾按下过Shift键"  ElseIf Shift = 2 ...

    GSM MODEM 动态链接库 (DLL)

    2、Sms_Send(Sms_TelNum As String, Sms_Text As String) As Integer Sms_Send函数说明如下: 功能描述:发送短信 Sms_TelNum:发送给的终端号码 Sms_Text:发送的短信内容 Sms_Send:返回值(0:发送短信失败...

    c++课件

    //p为指向函数的指针(即*p为函数名),该函数 //有两个int型参数, 且返回值类型也为int int a,b,c; char tmp; cout; //提示用户输入: 1 -- 求max; 0 -- 求min cin&gt;&gt;tmp; if(tmp=='1...

    Java进制转换方法整理

    返回值 10进制转2进制 Integer.toBinaryString(n) 一个二进制字符串 10进制转8进制 Integer.toOctalString(n) 一个八进制字符串 10进制转16进制 Integer.toHexString(n) 一个十六进制字符串 10进制转 R ...

    java发送短信之AT指令

    public void setId(Integer id) { this.id = id; } public String getSender() { return sender; } public void setSender(String sender) { this.sender = sender; } public String getRecver() { ...

    vb 万年历代码

    Dim AddMonth As Integer, AddDay As Integer, AddYear As Integer, getDay As Integer Dim RunYue As Boolean If tYear &gt; 2010 Or tYear 如果不是有效有日期,退出 \'1900 to 2009 dateList(1900) = \...

    短信猫C#程序

    2、Sms_Send(Sms_TelNum As String, Sms_Text As String) As Integer Sms_Send函数说明如下: 功能描述:发送短信 Sms_TelNum:发送给的终端号码 Sms_Text:发送的短信内容 Sms_Send:返回值(0:发送短信失败...

    Delphi多线程编程之三 同步读写全局数据

    GlobalArry:array[1..MaxSize] of Integer; Lock:byte; //1-不同步 2-临界区 3-互斥 CS:TRTLCriticalSection; //临界区 hMutex:THandle; //互斥 function GetNextNumber:Integer; begin Result:=Next...

    vb贪食蛇源代码

    Private Sub chkWindowButton_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single) If Button &lt;&gt; 1 Then Exit Sub Select Case Index Case 0 '锁定窗体 If Me....

    学习DWR JavaScript函数整理

    返回值 true :含有非法字符 false:没有指定的非法字符 ******************************************/ function chksafe( ParseString ) { fibdn = new Array ("\'" ,"\\", "、", ",", ";", "/","\&","$","~","!",...

    powerbuilder

    指定椭圆或圆外形框的宽度,以千分之一英寸为单位height:integer类型,指定椭圆或圆外形框的高度,以千分之一英寸为单位thickness:integer类型,指定椭圆或圆外边线的厚度,以千分之一英寸为单位返回值Integer。...

    VB 调色板应用程序.rar

     Static r As Integer, g As Integer, b As Integer '声明三个静态变量  h0 = HScroll1(0).Value '返回滑块当前位置的值  h1 = HScroll1(1).Value '将返回值分别放到三个变量里暂存  h2 = HScroll1(2).Value  ...

    围棋的控件

    类型:integer   说明:返回白棋提到的黑棋数量 1.1.4 BlackTiNumber  类型:integer  说明:返回黑棋提掉的白棋数量 1.1.5 GlobeErrMessage  类型:String  说明:返回最后一个错误的...

    二手房销售管理信息系统

    '返回值:超过返回值为True,否则为false ' ****************************************************************************** Public Function ISEquelLen(ByVal txt As TextBox, intlen As Integer) As Boolean ...

    PC机与GSM短信模块串口通信

    '假如没有找到成对出现或者任何一个都没有的返回值,那么要等待后面的内容 If Len(g_strSave) - iTmp1 &gt; 15 Then blHasError = True Else blHasError = False g_blIsWaiting = True End If End If Else If...

Global site tag (gtag.js) - Google Analytics