Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Object Equals method





I ran following example from link

http://www.jchq.net/certkey/0502certkey.htm

public class MyParm{
public static void main(String argv[]){
        Object m1 = new Object();
        Object m2 = new Object();
        System.out.println(m1);
        System.out.println(m2);
        if (m1.equals(m2)){
                System.out.println("Equals");
                }else{
                System.out.println("Not Equals");
                }
        }                
}
got output like
java.lang.Object@e48e1b
java.lang.Object@12dacd1
Not Equals


I was not clear why equlas will call toString() method and why toString() method gets called lot of times.




Any ideas, resources,sample code,links,  highly appreciated. thanks in advance.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

>        System.out.println(m1);
>        System.out.println(m2);

These two lines call toString() to get a string representation of the object to output

This is a good explanation of toString() method and also gives
details on default implementation of toString() for Object:

http://www.javabeginner.com/learn-java/java-tostring-method
equals method does not invoke toString() method. when you are trying to print some object, toString() method gets invoked to print the object.  
Avatar of gudii9

ASKER

>>>

class PointCoordinates {

      private int x, y;
      public PointCoordinates(int x, int y) {
            this.x = x;
            this.y = y;
      }
      public int getX() {
            return x;
      }
      public int getY() {
            return y;
      }
}

public class ToStringDemo {

      public static void main(String args[]) {
            PointCoordinates point = new PointCoordinates(10, 10);
            // using the Default Object.toString() Method
            System.out.println("Object toString() method : " + point);
            // implicitly call toString() on object as part of string concatenation
            String s = point + " testing";
            System.out.println(s);
      }
}

Download ToStringDemo.java

When you run the ToStringDemo program, the output is:
Object toString() method : PointCoordinates@1ad086a
PointCoordinates@1ad086a testing



>>i have gone through the link. I have not understood example and output there. how testing printed at last. please advise.
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>             System.out.println(s);

this method calls the String.valueOf() method like this:


        public void println(Object obj) {
           println(String.valueOf(obj));
       }

The valueOf() method then calls toString() to return a string representation


     public static String valueOf(Object value) {
         return value != null ? value.toString() : "null";
     }


Let me know if you have any questions
Avatar of gudii9

ASKER

>>>then compiler will undrstand it
as

Strinng s = p.toString() + " testing';

>>>System.out.println(s);

this method calls the String.valueOf() method like this:


        public void println(Object obj) {
           println(String.valueOf(obj));
       }

The valueOf() method then calls toString() to return a string representation


     public static String valueOf(Object value) {
         return value != null ? value.toString() : "null";
     }


is printing weird

PointCoordinates@1ad086a testing

Why is it not able to print something meaningful for point. please advise

output.
> Why is it not able to print something meaningful for point. please advise

Because PointCoordinates does not have a toString() method
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial