/** * Constructs a newly allocated {@code Integer} object that * represents the specified {@code int} value. * * @param value the value to be represented by the * {@code Integer} object. */ public Integer(int value) { this.value = value; } 上面是Integer的构造器, 第三个问题, ==操作符是引用比较, new两次, 会在堆中创建两个对象, 引用不相等, 所以返回false /** * 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); }上面是Integer.valueOf的源码, 可以看出, java对一些数字进行了缓存处理, 3在缓存范围内, 所以, 每次valueOf(3)返回的都是缓存中的对象.
true,true,false,true
个人觉得还是比较值还是比较内存地址的问题
true
false
true
我刚看了,感觉都是true,尤其是第三个。
刚运行了一下,就第三个不对。呵呵!我想起来引用类型等价时只有指向同一对象才是true,
应该用equals().
刚看完一本java请楼主指点,如何继续学下去?
自己还遍不了实例!
第一个和第二个是自动封箱/拆箱的结果第三个new 出来的内存地址不同第四个 是从IntegerCache.cache[]中取的数据,不是新new的所以true
System.out.println(new Integer(3) == new Integer(3));
new出来的两个对象,地址肯定不一样
System.out.println(Integer.valueOf(3) == Integer.valueOf(3));
Integer内部缓存了-128到127共256个Integer对象,valueOf如果在这个范围以内,返回的是同一个对象,如果不在这个范围以内,new一个返回
true
false
true
其他比较的是值。
true
false
true
第四个 ~
true 自动装箱
false 两个不同的对象
true 看了下源代码是做了缓存
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
不过为什么是i >= -128 && i <= 127涅,有什么意义等待高手解释
2.true:自动拆箱
3.false:比较的是地址,new就会开辟新内存所以不同
4.true:valueof返回的是值
true 自动装箱
false 地址比较,2个不同的对象地址不同
true 从IntegerCache.cache[]中取的数据,不是new的新对象,所以相同
2.true:自动拆箱 都转化为int
3.false:比较的是地址,new创建的是对象 变量存储的是对象的地址引用
4.true:valueof返回也是对象
只是Integer内部缓存了-128到127这个范围的Integer对象,valueOf如果在这个范围以内,返回的是同一个对象,如果不在这个范围以内,new一个返回新的Integer对象!!
valueOf
public static Integer valueOf(int i)返回一个表示指定的 int 值的 Integer 实例。如果不需要新
的 Integer 实例,则通常应优先使用该方法,而不是构造方法 Integer(int),因为该方法有可能通过缓存
经常请求的值而显著提高空间和时间性能。
//关于IntegerCache 是内部静态类 缓存的范围可能是为了统一
//而且这个范围用的也比较多,用空间来提高时间效率
private static class IntegerCache {
private IntegerCache(){} static final Integer cache[] = new Integer[-(-128) + 127 + 1]; static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}// ByteCache 是完全都缓存
private static class ByteCache {
private ByteCache(){} static final Byte cache[] = new Byte[-(-128) + 127 + 1];//这儿和Integer是一样的大小 static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Byte((byte)(i - 128));
}
public static Byte valueOf(byte b) {
final int offset = 128;
return ByteCache.cache[(int)b + offset];
} private static class CharacterCache {
private CharacterCache(){} static final Character cache[] = new Character[127 + 1];//这儿大小又有点不同 static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Character((char)i);
}
} public static Character valueOf(char c) {
if(c <= 127) { // must cache
return CharacterCache.cache[(int)c];
}
return new Character(c);
}//---------------------------------------------------------------------------
// 这些都说明了什么 说明Java在尽可能的、有意义的、合理的、有相对统一的情况下设计Cache
//---------------------------------------------------------------------------
System.out.println(new Integer(3) == 3);
生成的字节码:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: iconst_3
4: new #3; //class java/lang/Integer
7: dup
8: iconst_3
9: invokespecial #4; //Method java/lang/Integer."<init>":(I)V
12: invokevirtual #5; //Method java/lang/Integer.intValue:()I------>自动拆箱
15: if_icmpne 22
18: iconst_1
19: goto 23
22: iconst_0
23: invokevirtual #6; //Method java/io/PrintStream.println:(Z)V
26: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
29: new #3; //class java/lang/Integer
32: dup
33: iconst_3
34: invokespecial #4; //Method java/lang/Integer."<init>":(I)V
37: invokevirtual #5; //Method java/lang/Integer.intValue:()I------>自动拆箱
40: iconst_3
41: if_icmpne 48
44: iconst_1
45: goto 49
48: iconst_0
49: invokevirtual #6; //Method java/io/PrintStream.println:(Z)V
52: return
2.true:
3.false:比较的是地址,new就会开辟新内存所以不同
4.true:java中==比较的是内存地址。数字1、2、3……都是固定的内存地址。所以true
3的左右两边都是new。new声名的是新的内存地址,所以会出现false。
true
false
true
另外老紫竹提的问题很有水平,大家回答的也很热闹。
感觉自己又收获了一点点
其它的都是值比较,
基本类型与包装类比较应该是转换成基本类型比较。
包装类与包装类比较则是类之间的比较,由于地趾不同所以为false
第四个,听说0-128的数好像会缓存的,可能返回的两个Integer是同一个吧。
但是如果超过了缓存的范围就是false了。 System.out.println(Integer.valueOf(333) == Integer.valueOf(333));
intValue 才是值
* Constructs a newly allocated {@code Integer} object that
* represents the specified {@code int} value.
*
* @param value the value to be represented by the
* {@code Integer} object.
*/
public Integer(int value) {
this.value = value;
}
上面是Integer的构造器, 第三个问题, ==操作符是引用比较, new两次, 会在堆中创建两个对象, 引用不相等, 所以返回false
/**
* 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);
}上面是Integer.valueOf的源码, 可以看出, java对一些数字进行了缓存处理, 3在缓存范围内, 所以, 每次valueOf(3)返回的都是缓存中的对象.
True
False
True
这个题目就是考察对象的引用和储存在栈堆这些数据池离得东西.是对象还是引用的地址.
-128~127之间的全部int值都变成了引用类型Integer,而且都放到常量池中,而且第一个用Integer创造相应-128~127之间的int值放到常量池中,当然下次再出现同样的int值就是直接引用常量池中对应的同值引用。而所有的计算结果也都是相应的Integer中的值
事实上java中的基础类型变量都是被处理为唯一的常量引用.这比String还要强,String还可以用new创建新的同内容的字符串,而基本类型同值只有一个也就是说java中的所有的==都是比较地址,包括基本数据类型也是比较地址,而不是有的人理解的比较值换句话说,基本数据类型其实也是引用地址。例如int a=1;int b=2;a++;之后a和b指向的就是同一个地址,里面的内容就是2
前两个自动拆箱装箱好理解。。得三个都是new出来的,内存地址当然不相同咯!
第四个有点困难。
true true false true
第一个和第二个是自动封箱/拆箱的结果 第三个new 出来的内存地址不同 第四个 是从IntegerCache.cache[]中取的数据,不是新new的所以true
.true:自动拆箱 都转化为int
2.true:自动拆箱 都转化为int
3.false:比较的是地址,new创建的是对象 变量存储的是对象的地址引用
4.true:valueof返回也是对象
只是Integer内部缓存了-128到127这个范围的Integer对象,valueOf如果在这个范围以内,返回的是同一个对象,如果不在这个范围以内,new一个返回新的Integer对象!! 这此答案都可以吧!!!
先给答案
false
true
false
true
然后再去eclipse中 验证下