Integer c = 3;
    Integer d = 3;
    Integer e = 321;
    Integer f = 321;    System.out.println(c == d);
    System.out.println(e == f);输出 true false
    Integer   相等Java

解决方案 »

  1.   

    小于128的会用常量池中的 不会new Integer,大于128会new Integer,这时==比较的是什么?我想你懂的!
      

  2.   

    用javap指令看下,你就知道原因了
      

  3.   

    “==”比较的应该是hashcode吧,但是源码中的hashcode即value,也就是初始化时的int值;为什么大于128的就会new Integer呢
    javap指令怎么用?
      

  4.   

    static final Integer cache[] = new Integer[-(-128) + 127 + 1]; static {
        for(int i = 0; i < cache.length; i++)
    cache[i] = new Integer(i - 128);
    }
        }
    这是源码中的,也就是说cache中已有-128到127,不在这范围的会新new ,这时可以理解比较是内存地址,也就是是不是同一对象,并不是比较hashCode,hashcode相同对象不一样相同,毕竟hashcode是int大小是有限的,看来lz的==,equal,hashcode什么的还有待提高哈。。
      

  5.   

    c,d是基本数据类型,c==d就是比较它们的数值。
    而e,f是对象,此时用e==f比较的是e,f所指向的内存地址,结果是false。
    对象的比较时,要覆写hashcode和equals方法。记得以前时是这么看到的。
      

  6.   

    谁跟你说比的是hashcode,抓来抽四十大板
      

  7.   

    准确来说是在IntegerCache类中,谢谢,张见识了我知道“==”是比较地址的,之前看Object类中的hashCode计算是跟内存地址相关的,
    故以为hashcode可以像地址一样表示唯一性,原来后来的类有的会重写hashCode方法,
    那么就不能根据hashCode值来保证地址的唯一性
    其实hashCode跟equals方法保持高度的一致性,hashCode值的唯一性跟equals方法
    判断出的唯一性是一致的,故若子类重写了equals方法必重写hashCode方法,
    这次说对了吧?你们这群人,现在体罚学生都是犯法的,何况体罚楼主
    还有一题,各位若感兴趣,给个解释
    public static void main(String[] args)
    {
        Point p1 = new Point(0, 0);
        Point p2 = new Point(0, 0);
        modifyPoint(p1, p2);
        System.out.println("[" + p1.x + "," + p1.y + "], [" + p2.x + "," + p2.y + "]");
    } private static void modifyPoint(Point p1, Point p2)
    {
        Point tmpPoint = p1;
        p1 = p2;
        p2 = tmpPoint;
        p1.setLocation(5, 5);
        p2 = new Point(5, 5);
    }P1为何是[0,0]?
      

  8.   

    128是寄出的分界线,多学习integer应该你懂得
      

  9.   

    main方法中的p1,p2的引用没有因为调用modifyPoint()而发生改变!在modifyPoint()中,p1-->p2,p1.setLocation(5,,5)修改了main()中p2指向的point对象的值,所以main()p2所指向的对象属性改变了,在modifyPoint()中,p2-->tmpPoint(指向的是main()中p1所引用的对象),但是p2 = new Point(5, 5)之后在modifyPoint()中p2指向了堆中新建的point对象了,原p1(main()中)所指向的对象没有改变.
      

  10.   

    直接给Integer对象赋值的时候实际调用了valueOf方法,,只要查看valueOf方法就知道其中的奥秘了
      

  11.   

     4个 hashcode 相等说明了什么?String s = new String("abcde");  
    String s1 = "abcde"; 
    String s3 = "abcde";  
    String s2 = new String("abcde"); 
      

  12.   

     public void setLocation(int x, int y) {
     
    this.x = x;
    this.y = y;
    }
    <1>. private static void modifyPoint(Point p1, Point p2)
    {
       /* Point tmpPoint = p1;
        p1 = p2;
        p2 = tmpPoint;*/
        p1.setLocation(5, 5);
        p2 = new Point(5, 5);
    }
    结果是[5,5], [0,0]<2>. private static void modifyPoint(Point p1, Point p2)
    {
        Point tmpPoint = p1;
        p1 = p2;
        p2 = tmpPoint;
        p1.setLocation(5, 5);
        p2 = new Point(5, 5);
    }
    结果是[0,0], [5,5]咋回事?哪位大师解释一下?
      

  13.   

    Java的Reference问题
    Java的对象在堆中,Java在方法在执行时,会为每个方法创建栈帧,并压入当前线程的栈,每个栈帧都保存有方法的变量,字节码,引用等信息,以<1>为例子,P1,P2在main方法的栈帧中很老实的指向堆中的两个对象。而在modifyPoint栈帧中,p2 = new Point(5,5);创建了一个新的对象,并将该栈帧中的p2指向堆中的新对象。 
    注意,此问题的根本原因是两个方法中的P2是指向同一个堆对象的不同引用,对引用本身的更改,在不同的栈帧中是不相影响的。
    对于<2>也同样分析即可。
      

  14.   

    Thank you. p1.setLocation(5, 5);
     p2 = new Point(5, 5);
     
    有什么不同呢?
      

  15.   

    可以这样理解下,Integer是int的包装类,不是基本数据类型,是放在堆中的,像上面一些童鞋说的那样小于128的会有共享的行为,使用“==”比较的是两个int的包装类对象,比较的是堆中的引用地址,大于128的也是比较的引用的地址,但是大于128的地址没有共享的行为;
      

  16.   

    java的基本类型和String一样会将一定范围内且使用频繁的数据放到虚拟机的常量池中(百度面试官说的)对于Integer来说是:-128 到 +127
      

  17.   

    其不同在于 p1.setLocation(5, 5);本质上修改P1对象本身,修改了堆中的P1对象的属性
    而 p2 = new Point(5, 5);则修改了引用P2的值,执行前和扫行后,引用P2指向的堆内存区域是不同的
    你打印一下这两个引用的值就明白了
    System.out.println(p1);
     p1.setLocation(5, 5);
    System.out.println(p1);
    System.out.println(p2);
     p2 = new Point(5, 5);
    System.out.println(P2);
      

  18.   

    其实这种事情,直接进入Integer查看源码就好了。
    static final Integer cache[] = new Integer[-(-128) + 127 + 1]; static {
        for(int i = 0; i < cache.length; i++)
    cache[i] = new Integer(i - 128);
    }
      

  19.   

    其不同在于 p1.setLocation(5, 5);本质上修改P1对象本身,修改了堆中的P1对象的属性
     
    public static void main(String[] args)
    {
        Point p1 = new Point(0, 0);
        Point p2 = new Point(0, 0);
        
        Test1 test1 = new Test1();
        System.out.println(p1);
        System.out.println(p2);
        test1.modifyPoint(p1, p2);
        System.out.println(p1);
        System.out.println(p2);
        System.out.println("[" + p1.x + "," + p1.y + "], [" + p2.x + "," + p2.y + "]");
    } public   void modifyPoint(Point p1, Point p2)
    {
        /*Point tmpPoint = p1;
        p1 = p2;
        p2 = tmpPoint;*/
        
        
        System.out.println("P1 IN modifyPoint:" + p1);
        p1.setLocation(5, 5);
        System.out.println("P1 IN modifyPoint:" +p1);
       
        System.out.println("P2 IN modifyPoint:" +p2);
        p2 = new Point(5, 5);
        System.out.println("P2 IN modifyPoint:" +p2);
            
    }
    结果是:
    refOrValue.Point@2f9ee1ac
    refOrValue.Point@67f1fba0
    P1 IN modifyPoint:refOrValue.Point@2f9ee1ac
    P1 IN modifyPoint:refOrValue.Point@2f9ee1ac
    P2 IN modifyPoint:refOrValue.Point@67f1fba0
    P2 IN modifyPoint:refOrValue.Point@3fbefab0
    refOrValue.Point@2f9ee1ac
    refOrValue.Point@67f1fba0
    [5,5], [0,0]
      

  20.   

    P2指向的堆内存区域首地址: refOrValue.Point@67f1fba0  ---->  refOrValue.Point@3fbefab0明白了,太谢谢了
      

  21.   

    public static void main(String[] args)
    {
        Point p1 = new Point(0, 0);
        Point p2 = new Point(0, 0);
        
        Test1 test1 = new Test1();
        
        test1.modifyPoint(p1, p2);
        
        System.out.println("[" + p1.x + "," + p1.y + "], [" + p2.x + "," + p2.y + "]");
    } public   void modifyPoint(Point mp1, Point mp2)
    {
        mp1.setLocation(5, 5);
        mp2 = new Point(5, 5);
            
    }