论坛首页 Java企业应用论坛

蛋疼的JAVA比较相等符号

浏览 16756 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (3) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-10-17  
true
false
true
0 请登录后投票
   发表时间:2011-10-17  
原因是 从 -127 至 127 看看JDK的代码就知道了,-127至127都是已经初始化好了的
0 请登录后投票
   发表时间:2011-10-17  
这个问题 之前确实没扎注意过 这是什么版本出现的问题 还是所有的版本都一样
0 请登录后投票
   发表时间:2011-10-17  
chengxj_1986 写道
Integer类型 默认-128~127使用缓存数据, 在默认的范围内使用的是同一对象,所以相等,否则不等
    /**
     * Returns a <tt>Integer</tt> instance representing the specified
     * <tt>int</tt> value.
     * If a new <tt>Integer</tt> 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.
     *
     * @param  i an <code>int</code> value.
     * @return a <tt>Integer</tt> instance representing <tt>i</tt>.
     * @since  1.5
     */
    public static Integer valueOf(int i) {
        if(i >= -128 && i <= IntegerCache.high)
            return IntegerCache.cache[i + 128];
        else
            return new Integer(i);
    }

正解
0 请登录后投票
   发表时间:2011-10-17  
陷阱源于无知
0 请登录后投票
   发表时间:2011-10-17  
嘛。继续推一下我以前的一帖:[FYI] 关于Integer的自动缓存大小
0 请登录后投票
   发表时间:2011-10-17   最后修改:2011-10-17
编辑:楼上有人回答了 ,没看清:-) 。


学习 。
0 请登录后投票
   发表时间:2011-10-17  
享元模式罢了。。。
0 请登录后投票
   发表时间:2011-10-17  
真变态的题。。。
0 请登录后投票
   发表时间:2011-10-17  
受教了,受教了
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics