Simple question

A

ankur

Integer a1 = new Integer(54);
Integer a2 = new Integer(54);
Object oo = a1;

Why should this:

System.out.println(oo.equals(a2));

return "true":

When this:
System.out.println(a1==a2);

returns "false".

After all Object.equals() does comparison by doing

return (this == argObj)

Thanks,
Ankur
 
J

Joshua Cranmer

ankur said:
After all Object.equals() does comparison by doing

return (this == argObj)

It's actually Integer::equals() that is being called. This is what is
known as polymorphism; since the object in question is actually an
Integer, it calls Integer's version of the method and not Object's version.
 
A

ankur

It's actually Integer::equals() that is being called. This is what is
known as polymorphism; since the object in question is actually an
Integer, it calls Integer's version of the method and not Object's version.

Sorry for these basic questions , but I am kind of confused:
Consider this:

public class TestClass {

public static void main(String[] args) {
}
public boolean equals (Object o){
System.out.println("This is my equals");
return true;
}

}

TestClass s1 = new TestClass();
TestClass s2 = new TestClass();
Object o = s1;
System.out.println(o.equals(s2)); //gives false. Shouldn't this give
true going by your answer above
System.out.println(o== s2); // gives false as well

What is happening here ?

-A
 
J

Joshua Cranmer

ankur said:
Consider this:

public class TestClass {

public static void main(String[] args) {
}
public boolean equals (Object o){
System.out.println("This is my equals");
return true;
}

}

TestClass s1 = new TestClass();
TestClass s2 = new TestClass();
Object o = s1;
System.out.println(o.equals(s2)); //gives false. Shouldn't this give
true going by your answer above

It gives true for me.
 
A

ankur

public class TestClass {
public static void main(String[] args) {
   }
public boolean equals (Object o){
 System.out.println("This is my equals");
 return true;
}

TestClass s1 = new TestClass();
TestClass s2 = new TestClass();
Object o = s1;
System.out.println(o.equals(s2)); //gives false. Shouldn't this give
true going by your answer above
System.out.println(o== s2); // gives false as well
What is happening here ?

Your comments are obviously describing the behavior of code that's  
different from what you've actually posted.  After all, the class you  
posted does nothing at all, and the rest of the code you posted is out of  
context.  There's no way to get the code you posted to do what you say it  
does.

Without a concise-but-complete code example that reliably demonstrates the  
behavior you're describing, it's not possible to answer definitively.  
But, here's an example that actually _does_ what you obviously intended  
your example to do, but which behaves in exactly the way that Joshua  
explained:

public class TestOverrideEquals
{
     public static void main(String[] args)
     {
         Object o1 = new TestOverrideEquals();
         Object o2 = new TestOverrideEquals();

         System.out.println(o1.equals(o2));
         System.out.println(o1 == o2);
     }

     public boolean equals(Object o)
     {
         return true;
     }

}

This prints "true" and then "false" to the console.

If you have some code that is behaving contrary to your expectations, you  
need to post that code _exactly_.  Just copy-and-paste it from your actual  
code into your newsgroup post.

Pete

Pete , I got it. The thing was I was using two versions of TestClass
unknowingly. One was in a different package and the class where I was
generating the results was in a different package but had an import
statement to import TestClass from this other package. I commented it
out and everything is fine now..phew!

Thanks again,
Ankur
 
J

John B. Matthews

ankur said:
It's actually Integer::equals() that is being called. This is what is
known as polymorphism; since the object in question is actually an
Integer, it calls Integer's version of the method and not Object's version.
Sorry for these basic questions , but I am kind of confused:
Consider this:

public class TestClass {

public static void main(String[] args) {
}
public boolean equals (Object o){
System.out.println("This is my equals");
return true;
}
}

TestClass s1 = new TestClass();
TestClass s2 = new TestClass();
Object o = s1;
System.out.println(o.equals(s2)); //gives false. Shouldn't this give
true going by your answer above
System.out.println(o== s2); // gives false as well

What is happening here ?

You haven't shown TestClass. Does it, or an ancestor, override the
equals() method of Object? If not, you'll get Objects's equals(),
which is "the most discriminating possible."

<http://java.sun.com/javase/6/docs/api/java/lang/Object.html#equals(java.
lang.Object)>

If o is not identical to s2, false is the expected result.

[Please trim signature lines when replying.]
 
A

ankur

Sorry for these basic questions , but I am kind of confused:
Consider this:
public class TestClass {
public static void main(String[] args) {
   }
public boolean equals (Object o){
 System.out.println("This is my equals");
 return true;
}
}
TestClass s1 = new TestClass();
TestClass s2 = new TestClass();
Object o = s1;
System.out.println(o.equals(s2)); //gives false. Shouldn't this give
true going by your answer above
System.out.println(o== s2); // gives false as well
What is happening here ?

You haven't shown TestClass. Does it, or an ancestor, override the
equals() method of Object? If not, you'll get Objects's equals(),
which is "the most discriminating possible."

<http://java.sun.com/javase/6/docs/api/java/lang/Object.html#equals(java.
lang.Object)>

If o is not identical to s2, false is the expected result.

[Please trim signature lines when replying.]

TestClass is shown in my second post.

Thanks,
Ankur
 
J

John B. Matthews

ankur said:
 ankur said:
ankur wrote:
After all Object.equals()  does comparison by doing
return (this == argObj)
It's actually Integer::equals() that is being called. This is what is
known as polymorphism; since the object in question is actually an
Integer, it calls Integer's version of the method and not Object's
version.
Sorry for these basic questions , but I am kind of confused:
Consider this:
public class TestClass {
public static void main(String[] args) {
   }
public boolean equals (Object o){
 System.out.println("This is my equals");
 return true;
}
}
TestClass s1 = new TestClass();
TestClass s2 = new TestClass();
Object o = s1;
System.out.println(o.equals(s2)); //gives false. Shouldn't this give
true going by your answer above
System.out.println(o== s2); // gives false as well
What is happening here ?

You haven't shown TestClass. Does it, or an ancestor, override the
equals() method of Object? If not, you'll get Objects's equals(),
which is "the most discriminating possible."

<http://java.sun.com/javase/6/docs/api/java/lang/Object.html>

If o is not identical to s2, false is the expected result.

TestClass is shown in my second post.

Ah, I overlooked it and you misplaced it. It's a wily one!

[Please trim signature lines when replying.]
 
R

Roedy Green

Why should this:

System.out.println(oo.equals(a2));

return "true":

When this:
System.out.println(a1==a2);

returns "false".

see http://mindprod.com/jgloss/string.html#COMPARISON

Once you understand the difference between == and equals for String
objects, you should be able to understand Integer objects too.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Learning is not compulsory... neither is survival."
~ Dr. W. (William) Edwards Deming (born: 1900-10-14 died: 1993-12-20 at age: 93))
 
A

ankur

seehttp://mindprod.com/jgloss/string.html#COMPARISON

Once you understand the difference between == and equals for String
objects, you should be able to understand Integer objects too.
--
Roedy Green Canadian Mind Productshttp://mindprod.com

"Learning is not compulsory... neither is survival."
~ Dr. W. (William) Edwards Deming (born: 1900-10-14 died: 1993-12-20 at age: 93))

yes Roedy..thanks for ur help. I was inadvertently using a class from
some other package thats why I was getting weird results.

Ankur
 
A

Andreas Leitgeb

ankur said:
Sorry for these basic questions , but I am kind of confused:
Consider this:

public class TestClass {
public static void main(String[] args) {
}
public boolean equals (Object o){
System.out.println("This is my equals");
return true;
}
}

TestClass s1 = new TestClass();
TestClass s2 = new TestClass();
Object o = s1;
System.out.println(o.equals(s2)); //gives false. Shouldn't this give
true going by your answer above
System.out.println(o== s2); // gives false as well

What is happening here ?

The line with equals should indeed print "true" right after
the line "This is my equals". If it does not, then maybe
the compiler picked up a different TestClass than the one
you posted. Perhaps you just forgot to save and recompile
your TestClass.

Place the testcode (the one you placed after the class)
into the main() of the class, (thereby making it an SSCCE)
and retry. I tried it, and it works just as expected.
If it still doesn't for you, then your installation of
Java must be broken.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top