==和equals的区别,以及hashCode

==和equals的区别

  • 千古不变的:==

    • Java中,"= ="永远在比较2个对象是否是同一个对象(指向同一块内存:就是同一个对象);

    • 比较的就是内存地址;

 

  • 不确定的equals:

    • 讨论equals,要带上"类";

    • 其实,Java中的equal作为一个方法,我们是不能脱离了”类“而单独讨论它的;

    • 那么,说到底,equals方法来自Object类,源码如下:

      //Object类中的  equals方法
      public boolean equals(){
        return this ==obj;
      }

      /**
        总结:
        1,从源码中我们看到了equals和==深远的缘分;
        2,Object类中的equals方法与==功能是一模一样的:比较2个对象是否是同一个对象;
        3,只是,作为Object子类的String类为了符合自身的特质,重写了Object类中的equals方法;
        4,重写后的效果是:如果2个String对象的值相同,那么调用equals方法返回true,否则返回false;
         

        为什么要这么做:
        1,这是很有道理的,面向对象的思维方式其根源就是模拟现实生活的逻辑;
        2,在现实生活中,我们认为属性完全相同的2个对象就应该是同一个对象(比如String类的值);
        3,如果不这样,只用==就好了,为什么还要用equals?



      */

       

    • 代码案例:== 和 equals


      public class Test {
        public static void main(String[] args) {

            String str1="hello";
            String str2=new String ("hello");
            String str3=new String ("hello");
            String str4=str3;
            String str5=str1;

            System.out.println(str1==str2);//false
            System.out.println(str1==str3);//false
            System.out.println(str2==str3);//false
            System.out.println(str1==str4);//false
            System.out.println(str1==str5);//true

            System.out.println("----------------");
            System.out.println(str1.equals(str2));//true
            System.out.println(str1.equals(str3));//true
            System.out.println(str2.equals(str3));//true
            System.out.println(str1.equals(str4));//true
            System.out.println(str1.equals(str5));//true
            System.out.println(str2.equals(str3));//true
            System.out.println(str3.equals(str4));//true



        }
      }

 

 

  • hashCode比较:

    String类:
    * 1,重写hashCode()方法:如果值相同,hashCode值必须相同;
    * 2,跟内存地址没有关系;
  • 代码:


    /**
    * String类:
    * 1,重写hashCode()方法:如果值相同,hashCode值必须相同;
    * 2,跟内存地址没有关系;
    */
    public class Test {
       public static void main(String[] args) {

           String s1="haha";
           String s2="haha";
           String s3=s1;
           String s4=new String("haha");
           String s5=new String("haha");
           String s6=s4;

           System.out.println("=:--------------------");
           int i1=s1.hashCode();
           int i2=s2.hashCode();
           int i3=s3.hashCode();
           int i4=s4.hashCode();
           int i5=s5.hashCode();
           int i6=s6.hashCode();

           System.out.println(i1);//3194802//i1==i2==i3==i4==i6==i5
           System.out.println(i2);//3194802
           System.out.println(i3);//3194802
           System.out.println(i4);//3194802
           System.out.println(i5);//3194802
           System.out.println(i6);//3194802



      }
    }

    /*
           System.out.println("=:--------------------");
           System.out.println(s1==s2);//true
           System.out.println(s1==s3);//true
           System.out.println(s1==s4);//false
           System.out.println(s1==s5);//false
           System.out.println(s1==s6);//false
           System.out.println("=:--------------------");
           System.out.println(s2==s3);//true
           System.out.println(s2==s4);//false
           System.out.println(s2==s5);//false
           System.out.println(s2==s6);//false
           System.out.println("=:--------------------");
           System.out.println(s3==s4);//false
           System.out.println(s3==s5);//false
           System.out.println(s3==s6);//false
           System.out.println("=:--------------------");
           System.out.println(s4==s5);//false
           System.out.println(s4==s6);//true
           System.out.println("=:--------------------");
           System.out.println(s5==s6);//false


           ------------------

           System.out.println("=:--------------------");
           System.out.println(s1.equals(s2));//true
           System.out.println(s1.equals(s3));//true
           System.out.println(s1.equals(s4));//true
           System.out.println(s1.equals(s5));//true
           System.out.println(s1.equals(s6));//true
           System.out.println("=:--------------------");
           System.out.println(s2.equals(s3));//true
           System.out.println(s2.equals(s4));//true
           System.out.println(s2.equals(s5));//true
           System.out.println(s2.equals(s6));//true
           System.out.println("=:--------------------");
           System.out.println(s3.equals(s4));//true
           System.out.println(s3.equals(s5));//true
           System.out.println(s3.equals(s6));//true
           System.out.println("=:--------------------");
           System.out.println(s4.equals(s5));//true
           System.out.println(s4.equals(s6));//true
           System.out.println("=:--------------------");
           System.out.println(s5.equals(s6));//true

           --------------


    */
posted @   gzs1024  阅读(79)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示