操作符“==”与对象的equals()方法

来源:互联网 发布:java动态添加定时任务 编辑:程序博客网 时间:2024/06/01 08:08

       有些同学对操作符“==”和对象的equals()方法不是很了解,我对它俩的用法也不是很明了,看了孙伟琴老师的《java面向对象编程》里关于“==”和equals()的应用与区别。现总结一下,便于自己以后参阅以及有需要的同学参考。

一、  操作符“==”用来比较两个操作元是否相等,这两个操作元既可以是基本类型,也可以是引用类型;equals()方法定义在java.lang.Object类中,用来比较两个对象是否相等。

     当操作符“==”两边都是引用类型变量时,这两个引用变量必须都引用同一个对象,结果才为true。

     Integer int 1 = new Integer(1);

     Integer int 2= new Integer(1);

     Integer int 3 = int 1; // int3和int1引用同一个对象

 

    int [ ] array1 = new int [1];

   int [ ] array2 = new int [1];

   int [ ] array3 = array1;  // array3和array1引用同一个数组

 

   System.out.println("int1==int2 is "+( int1==int2));      

   System.out.println("int1==int3 is "+( int1==int3));  

   System.out.println("array1==array2 is "+( array1==array2)); 

   System.out.println("array1==array3 is "+( array1==array3));  

运行上面的程序,打印结果如下:

       int1==int2 is false   

       int1==int3 is true

      array1==array2 is false

      array1==array3 is true  

1、操作符“==”与多态性

      对于引用类型的变量,Java编译器根据变量被显示声明的类型去编译。当“==”用于比较引用类型变量时,“==”两边的变量被显示声明的类型必须是同种类型或有继承关系,即位于继承树的同一分支上,否则编译报错,四个类Creature、Animal、Dog和Cat类,它们之间的继承关系为:Animal继承自Creature,Dog和Cat继承自Animal

     Dog dog = new Dog();   //  dog被声明为Dog类型

     Creature creature = dog; //变量creature和dog引用同一个Dog对象

     Animal animal = new Cat(); //animal被声明为Animal类型

    System.out.println(dog==animal); //合法,打印false

    System.out.println(dog==creature); //合法,打印true

以下代码中变量dog被声明为Dog类型,cat被声明为Cat类型,Dog类和Cat类之间没有继承关系,因为这两个变量不能用“==”比较。

  Dog dog = new Dog();

 Cat cat = new Cat();

 System.out.print(dog==cat); //编译出错

2、操作符“==”用于数组类型

       数组类型为引用类型,也可用“==”进行比较,

      boolean b1 = new int[4] ==new long[5] ; //编译出错,两边的类型不一致

      boolean b1 = new int[4] ==new int[4];  //合法,b2为false

      int [ ] array1 = new int [4];

     int [ ] array2 = array1;

     boolean b3 = array1==array2; //合法,b3的值为true

 

二、equals()方法是定义在Object类中的方法,声明格式为: public boolean equals(Object obj)

      Object类的equals() 方法的比较规则是:当参数obj引用的对象与当前对象是同一个对象时,返回true,否则返回false。

                          public boolean equals (Object obj){

                                  if(this==obj) return true;

                                      else return false;

                                    } 

              Animal animal1 = new Dog();

              Animal  animal2 = new Cat();

              Animal animal3 = animal1;

 

           System.out.print(animal1==animal2);  //false,animal1和animal2引用的是不同对象

            System.out.print(animal1.equlas(animal2));  //false,animal1和animal2引用的是不同对象

           System.out.print(animal1==animal3);  //true,animal1和animal3引用的是相同对象

           System.out.print(animal1.equlas(animal3));   //true,animal1和animal3引用的是相同对象

 

   JDK中有一些类覆盖了Object类的equals方法,他们的比较规则为:如果两个对象的类型一致,且内容一致,则返回true。

  这些类包括:java.io.File,java.util.Date,java.lang.String,包装类(java.lang.Integer和java.lang.Double类)等。

                                              Integer int1 = new Integer(1);

                                              Integer int1 = new Integer(1);

 

                                              String str1 = new String("Hello");'

                                              String str2= new String("Hello");

 

                                              System.out.print(int1==int2);  //false  int1与int2引用的不是相同对象

                                              System.out.print(int1.equals(int2)); //true java.lang.Integer复写了Object中的equals()方法,如果两个对象的类型一致,且内容一致,则返回true。

 

                                              System.out.print(str1==str2);//false str1与str2引用的不是相同对象

                                              System.out.print(str1.equals(str2));//true  java.lang.String复写了Object中的equals()方法,如果两个对象的类型一致,且内容一致,则返回true。

 

总结:在实际运用中,比较字符串是否相等时,通常是按照内容来比较才有意义,应该调用String类的equals()方法,是不是采用“==”操作符,大家在使用时,一定要注意。

                          String name = new String("Tom");

                         //比较结果为false,应该把“==”改为name.equals(“Tom”),比较结果才为true。

                         if(name==“Tom”){

                              System.out.print("Hello,Tom");

                            }else{

                              System.out.print("Sorry,I do not know you");                            

                            }

 

另外,用户也可以在自定义类中覆盖Object类的equals()方法,重新定义比较规则。 

原创粉丝点击