==和equals()的分析

/**
 * ==和equals区别
 * ==:    基本变量中(八大基本变量:byte,short,int,long,float,double,char,boolean),==操作符专门用来比较两个变量的值是否相等 ;
 *         引用变量中(String,Integer类重写了equals方法),==操作符专门用来比较两个变量的引用是否相等。
 * 
 * equals: 只能作用于引用变量,没有重写,则指向两个变量的引用地址,如果重写了,则指向两个变量的内容
 */
public class test2 {


    public static void main(String[] args) {

        //当为引用变量时
        String a = new String("你好");
        String b = new String("你好");
        System.out.println(a == b);//内容虽然一样,但是引用变量指向的地址不同,所以变量值不同。false
        System.out.println(a.equals(b));// 因为String类里重写了equals方法,所以比较的是内容,相同,为true

        String i = "你好";
        String j = "你好";
        System.out.println(i == j);//true
        System.out.println(i.equals(j));//true
        System.out.println(i.equals(b));//true

        test2 g = new test2();
        test2 h = new test2();
        System.out.println(g.equals(h));//false  因为自己定义的test2没有重写equals方法,
        // 所以比较的是地址,因为两个对象的地址都不同,所以为false

        // 当为基础类型时
        int c = 5;
        int d = 5;
        short e = 5;
        int f = c;
        System.out.println(c == d);//true
        System.out.println(c == e);//true
        System.out.println(f == c);//true


    }
}

下面是关于Integer的自动装箱和自动拆箱而写的一个小小的测试程序,代码如下:

1. public class Test{ 2. public static void main(String[] args) { 3. Integer a = new Integer( 200); 4. Integer b = new Integer( 200); 5. Integer c = 200; 6. Integer e = 200; 7. int d = 200; 8. 9. System.out.println( "两个new出来的对象 ==判断"+(a==b)); 10. System.out.println( "两个new出来的对象 equal判断"+a.equals(b)); 11. System.out.println( "new出的对象和用int赋值的Integer ==判断"+(a==c)); 12. System.out.println( "new出的对象和用int赋值的Integer equal判断"+(a.equals(c))); 13. System.out.println( "两个用int赋值的Integer ==判断"+(c==e)); 14. System.out.println( "两个用int赋值的Integer equal判断"+(c.equals(e))); 15. System.out.println( "基本类型和new出的对象 ==判断"+(d==a)); 16. System.out.println( "基本类型和new出的对象 equal判断"+(a.equals(d))); 17. System.out.println( "基本类型和自动装箱的对象 ==判断"+(d==c)); 18. System.out.println( "基本类型和自动装箱的对象 equal判断"+(c.equals(d))); 19. } 20.}
作者:bu_想 链接:https://www.jianshu.com/p/9cb9c61b0986 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1. public class Test{ 2. public static void main(String[] args) { 3. Integer a = new Integer( 200); 4. Integer b = new Integer( 200); 5. Integer c = 200; 6. Integer e = 200; 7. int d = 200; 8. 9. System.out.println( "两个new出来的对象 ==判断"+(a==b)); 10. System.out.println( "两个new出来的对象 equal判断"+a.equals(b)); 11. System.out.println( "new出的对象和用int赋值的Integer ==判断"+(a==c)); 12. System.out.println( "new出的对象和用int赋值的Integer equal判断"+(a.equals(c))); 13. System.out.println( "两个用int赋值的Integer ==判断"+(c==e)); 14. System.out.println( "两个用int赋值的Integer equal判断"+(c.equals(e))); 15. System.out.println( "基本类型和new出的对象 ==判断"+(d==a)); 16. System.out.println( "基本类型和new出的对象 equal判断"+(a.equals(d))); 17. System.out.println( "基本类型和自动装箱的对象 ==判断"+(d==c)); 18. System.out.println( "基本类型和自动装箱的对象 equal判断"+(c.equals(d))); 19. } 20.}
作者:bu_想 链接:https://www.jianshu.com/p/9cb9c61b0986 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1. public class Test{ 2. public static void main(String[] args) { 3. Integer a = new Integer( 200); 4. Integer b = new Integer( 200); 5. Integer c = 200; 6. Integer e = 200; 7. int d = 200; 8. 9. System.out.println( "两个new出来的对象 ==判断"+(a==b)); 10. System.out.println( "两个new出来的对象 equal判断"+a.equals(b)); 11. System.out.println( "new出的对象和用int赋值的Integer ==判断"+(a==c)); 12. System.out.println( "new出的对象和用int赋值的Integer equal判断"+(a.equals(c))); 13. System.out.println( "两个用int赋值的Integer ==判断"+(c==e)); 14. System.out.println( "两个用int赋值的Integer equal判断"+(c.equals(e))); 15. System.out.println( "基本类型和new出的对象 ==判断"+(d==a)); 16. System.out.println( "基本类型和new出的对象 equal判断"+(a.equals(d))); 17. System.out.println( "基本类型和自动装箱的对象 ==判断"+(d==c)); 18. System.out.println( "基本类型和自动装箱的对象 equal判断"+(c.equals(d))); 19. } 20.}
作者:bu_想 链接:https://www.jianshu.com/p/9cb9c61b0986 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1. public class Test{ 2. public static void main(String[] args) { 3. Integer a = new Integer( 200); 4. Integer b = new Integer( 200); 5. Integer c = 200; 6. Integer e = 200; 7. int d = 200; 8. 9. System.out.println( "两个new出来的对象 ==判断"+(a==b)); 10. System.out.println( "两个new出来的对象 equal判断"+a.equals(b)); 11. System.out.println( "new出的对象和用int赋值的Integer ==判断"+(a==c)); 12. System.out.println( "new出的对象和用int赋值的Integer equal判断"+(a.equals(c))); 13. System.out.println( "两个用int赋值的Integer ==判断"+(c==e)); 14. System.out.println( "两个用int赋值的Integer equal判断"+(c.equals(e))); 15. System.out.println( "基本类型和new出的对象 ==判断"+(d==a)); 16. System.out.println( "基本类型和new出的对象 equal判断"+(a.equals(d))); 17. System.out.println( "基本类型和自动装箱的对象 ==判断"+(d==c)); 18. System.out.println( "基本类型和自动装箱的对象 equal判断"+(c.equals(d))); 19. } 20.}
作者:bu_想 链接:https://www.jianshu.com/p/9cb9c61b0986 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1. public class Test{ 2. public static void main(String[] args) { 3. Integer a = new Integer( 200); 4. Integer b = new Integer( 200); 5. Integer c = 200; 6. Integer e = 200; 7. int d = 200; 8. 9. System.out.println( "两个new出来的对象 ==判断"+(a==b)); 10. System.out.println( "两个new出来的对象 equal判断"+a.equals(b)); 11. System.out.println( "new出的对象和用int赋值的Integer ==判断"+(a==c)); 12. System.out.println( "new出的对象和用int赋值的Integer equal判断"+(a.equals(c))); 13. System.out.println( "两个用int赋值的Integer ==判断"+(c==e)); 14. System.out.println( "两个用int赋值的Integer equal判断"+(c.equals(e))); 15. System.out.println( "基本类型和new出的对象 ==判断"+(d==a)); 16. System.out.println( "基本类型和new出的对象 equal判断"+(a.equals(d))); 17. System.out.println( "基本类型和自动装箱的对象 ==判断"+(d==c)); 18. System.out.println( "基本类型和自动装箱的对象 equal判断"+(c.equals(d))); 19. } 20.}
作者:bu_想 链接:https://www.jianshu.com/p/9cb9c61b0986 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1. public class Test{ 2. public static void main(String[] args) { 3. Integer a = new Integer( 200); 4. Integer b = new Integer( 200); 5. Integer c = 200; 6. Integer e = 200; 7. int d = 200; 8. 9. System.out.println( "两个new出来的对象 ==判断"+(a==b)); 10. System.out.println( "两个new出来的对象 equal判断"+a.equals(b)); 11. System.out.println( "new出的对象和用int赋值的Integer ==判断"+(a==c)); 12. System.out.println( "new出的对象和用int赋值的Integer equal判断"+(a.equals(c))); 13. System.out.println( "两个用int赋值的Integer ==判断"+(c==e)); 14. System.out.println( "两个用int赋值的Integer equal判断"+(c.equals(e))); 15. System.out.println( "基本类型和new出的对象 ==判断"+(d==a)); 16. System.out.println( "基本类型和new出的对象 equal判断"+(a.equals(d))); 17. System.out.println( "基本类型和自动装箱的对象 ==判断"+(d==c)); 18. System.out.println( "基本类型和自动装箱的对象 equal判断"+(c.equals(d))); 19. } 20.}
作者:bu_想 链接:https://www.jianshu.com/p/9cb9c61b0986 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
public class Test {     public static void main(String[] args) {         Integer a = new Integer(200);         Integer b = new Integer(200);         Integer c = 200;         Integer e = 200;         int d = 200;         System.out.println("两个new出来的对象 ==判断" + (a == b));         System.out.println("两个new出来的对象 equal判断" + a.equals(b));         System.out.println("new出的对象和用int赋值的Integer ==判断" + (a == c));         System.out.println("new出的对象和用int赋值的Integer equal判断" + (a.equals(c)));         System.out.println("两个用int赋值的Integer ==判断" + (c == e));         System.out.println("两个用int赋值的Integer equal判断" + (c.equals(e)));         System.out.println("基本类型和new出的对象 ==判断" + (d == a));         System.out.println("基本类型和new出的对象 equal判断" + (a.equals(d)));         System.out.println("基本类型和自动装箱的对象 ==判断" + (d == c));         System.out.println("基本类型和自动装箱的对象 equal判断" + (c.equals(d)));     } } 执行结果如下: //=======================================================
两个 new出来的对象 ==判断 false两个 new出来的对象 equal判断 true new出的对象和用 int赋值的Integer ==判断 false new出的对象和用 int赋值的Integer equal判断 true两个用 int赋值的Integer ==判断 false两个用 int赋值的Integer equal判断 true基本类型和 new出的对象 ==判断 true基本类型和 new出的对象 equal判断 true基本类型和自动装箱的对象 ==判断 true基本类型和自动装箱的对象 equal判断 true
作者:bu_想 链接:https://www.jianshu.com/p/9cb9c61b0986 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
        两个new出来的对象 ==判断false         两个new出来的对象 equal判断true         new出的对象和用int赋值的Integer ==判断false         new出的对象和用int赋值的Integer equal判断true         两个用int赋值的Integer ==判断false         两个用int赋值的Integer equal判断true         基本类型和new出的对象 ==判断true         基本类型和new出的对象 equal判断true         基本类型和自动装箱的对象 ==判断true         基本类型和自动装箱的对象 equal判断true //=======================================================
首先,第1个比较中,对于两个new出来的Integer对象,用==比较两者,得到了false,这点应该挺好理解的,每次使用new关键字,都会在堆 内存中申请一块空间,存放相应的对象的值,然后在栈中存放这块内存的引用。而==运算符比较两者所指向对象的地址是否相同,申请了 两块空间,地址肯定不相同,所以结果为false。 第2个比较中,结果为true,查了下java的源码,发现Integer重写的equals方法是这样的:
public boolean equals(Object obj) {
  if (obj instanceof Integer) {
    return value == ((Integer)obj).intValue();
  }
  return false;
}
又调用了一个intValue方法,它的实现是这样的:
public int intValue()  
{  
  return value;  
}  
首先判断传进来的Object类型是不是Integer类的一个实例,如果不是直接返回false;如果是则判断两者的成员变量value值是不是相等的
(Integer类中定义的private final int value),这块又回到了基本类型的比较。value的值在创建这个对象的时候被赋值,两个Integer对象
传递的参数都为200,所以value值相等,返回true。
看第3个比较前,先看下Test类中main方法第5行。第5行中,用int给Integer赋值的那条语句,从jdk1.5开始能这么做了,因为从这个版本开始,
java引入了自动装箱、自动拆箱机制。第5行就是一个自动装箱的过程,相当于: Integer c = Integer.valueOf(200);
在Integer类中,valueOf方法是这么实现的:

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); }


作者:bu_想
链接:https://www.jianshu.com/p/9cb9c61b0986
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
public static Integer valueOf(int i){
    assert IntegerCache.high >= 127;
    if (i >=IntegerCache.low && i <= IntegerCache.high)
        return IntegerCache.cahce[i+(-IntegerCache.low)];
    return new Integer(i);
}

上面这段代码首先规定了一个范围,默认是-128-127之间,如果参数中的i在这个范围之内,则返回一个数组中的内容,如果不在这个范围,则new
一个新的Integer对象并返回。查看Integer类的源码可以发现,这个数组里面缓存了基本类型-128-127之间的Integer对象。但是由于第11行是与
一个new出来的对象做比较,所以==肯定返回的false。
第4个比较行,equals方法比较两个对象的value值,所以为true。
第5个比较行,两个自动装箱的变量,但是装箱传递的值大于127,所以返回false。这这块又试了下在-128到127之间的数,结果为true,大家可以试下。
第6个比较行,结果为true。两个自动装箱的Integer对象,比较value。
第7个比较行,这块进行比较的时候,会对 Integer对象进行自动拆箱,也就是调用intValue方法,方法如上。两个基本数据类型进行==判断,根据值比较,
所以结果为true。这块可能有人会问,为什么不是对int类型进行自动装箱处理呢?其实这块是java根据一个很明显的道理进行设计的:如果有人比较
一个int类型的值和Integer类型的值,是想比较什么呢?肯定是值呀,所以这块是对Integer对象进行拆箱而不是对int类型装箱了。
第8个比较行这块,首先调用equals方法的肯定是Integer对象,但是Integer类中重写的equals方法参数是一个Object类型呀,怎么能传递一个基本数据类型
进去呢?所以这块又是一个自动装箱的表现, 当传递一个int类型给equals这个方法时,java会自动将这个值打包装箱为Integer类,而Integer类
的最终父类又是Object,所以这块参数的问题就解决了,然后就是两个Integer对象进行equals判断,返回true。
第9个比较行,首先d为一个基本类型int,c为一个Integer对象,所以进行==比较的时候,肯定会对Integer对象进行拆箱处理,所以结果为true。
第10个比较行,同第8个比较行。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

suppppper

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值