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.
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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

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
0
msk_apkCommented:
equals method does not invoke toString() method. when you are trying to print some object, toString() method gets invoked to print the object.  
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

gudii9Author Commented:
>>>

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.
0
for_yanCommented:

The frist piece of ocede was just showing that
default implementation toString() for the object
produces some weird strings which correspond to
some way of memory addres of the object in memory -
that's why it is not sensical

But look below there is a second piece of
code where the toString() method of class Point
is overriden - and then

it will print syuch string if we say p.toString() and p is instance of
Point with overriden tostring() class.

"X=" + x + " " + "Y=" + y;


They also explauin the when
you take any obect and
try to concatentae it with the String using "+

then java will use instead of the object
the result of the toString() method on that object,.

Therefore if your Point has
toString() object defined like uin second case
then

if you say
String s = p + " testing";
(and p is instance of Point)
then compiler will undrstand it
as

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

and p.toString() returns "X=" + x + " " + "Y=" + y;

so s will be equal to "X=" + x + " " + "Y=" + y + " testing"
(on real output there will be no quotes and x and y will
be relaced by real coordinates of this point

 







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;
	}
	// Custom toString() Method.
	public String toString() {
		return "X=" + x + " " + "Y=" + y;
	}
}

public class ToStringDemo2 {

	public static void main(String args[]) {
		PointCoordinates point = new PointCoordinates(10, 10);
		System.out.println(point);
		String s = point + " testing";
		System.out.println(s);
	}
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
objectsCommented:
>             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
0
gudii9Author Commented:
>>>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.
0
objectsCommented:
> Why is it not able to print something meaningful for point. please advise

Because PointCoordinates does not have a toString() method
0
objectsCommented:
More accurately it has not overridden the default toString() method (which just prints what you are seeing)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.