《深入理解Java虚拟机》中的阐述Java语法糖的有关自动装箱的例子如下:public class AutoPack { public static void main(String[] args) {
// TODO Auto-generated method stub
Integer a=1;
Integer b=2;
Integer c=3;
Integer d=3;
Integer e=321;
Integer f=321;
Long g=3L;
System.out.println("c==d is:"+(c==d));
System.out.println("e==f is:"+(e==f));
System.out.println("c==(a+b)d is:"+(c==(a+b)));
System.out.println("c.equals(a+b) is:"+(c.equals(a+b)));
System.out.println("g==(a+b) is:"+(g==(a+b)));
System.out.println("g.equals(a+b) is:"+(g.equals(a+b))); }}
输出结果如下:
c==d is:true
e==f is:false
c==(a+b)d is:true
c.equals(a+b) is:true
g==(a+b) is:true
g.equals(a+b) is:false疑问主要有两点:
1.c==d和e==f无论类型还是数值应该都是一样的,为什么c==d返回的是true,而e==f返回的false?
2c.equals(a+b)和g.equals(a+b)的自动装箱是怎么样的,和1有同样的疑问,返回的结果为什么是不同的?
// TODO Auto-generated method stub
Integer a=1;
Integer b=2;
Integer c=3;
Integer d=3;
Integer e=321;
Integer f=321;
Long g=3L;
System.out.println("c==d is:"+(c==d));
System.out.println("e==f is:"+(e==f));
System.out.println("c==(a+b)d is:"+(c==(a+b)));
System.out.println("c.equals(a+b) is:"+(c.equals(a+b)));
System.out.println("g==(a+b) is:"+(g==(a+b)));
System.out.println("g.equals(a+b) is:"+(g.equals(a+b))); }}
输出结果如下:
c==d is:true
e==f is:false
c==(a+b)d is:true
c.equals(a+b) is:true
g==(a+b) is:true
g.equals(a+b) is:false疑问主要有两点:
1.c==d和e==f无论类型还是数值应该都是一样的,为什么c==d返回的是true,而e==f返回的false?
2c.equals(a+b)和g.equals(a+b)的自动装箱是怎么样的,和1有同样的疑问,返回的结果为什么是不同的?
解决方案 »
- Runtime.getRuntime().exec()问题.
- 程序一共就俩句话,我怎么就不知道哪错了呢??
- jndi+ssl访问domino ldap报错"Bind failed: Username / password binds not allowed"?
- 如何在dom中加入xmlns属性?
- 大家帮帮我啊,请问怎么打印正方形啊,谢谢了,没分了
- 谁给我讲一下void的用法呢?
- 线程问题,拜托各位大虾帮忙,小生有理了:)
- 请教Clone问题!
- 请各位进来帮忙解答
- 我的Java Applet中用URL连接Delphi中的Socket Server出现Exception
- 书上给出的代码,有点看不懂啊~
- 对象为什么要经过构造函数初始化?
java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)
* 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. During VM initialization the
* getAndRemoveCacheProperties method may be used to get and remove any system
* properites that configure the cache size. At this time, the size of the
* cache may be controlled by the vm option -XX:AutoBoxCacheMax=<size>.
*/ // value of java.lang.Integer.IntegerCache.high property (obtained during VM init)
private static String integerCacheHighPropValue;
而不是Integer c = new Integer(3);