This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes .equals method for objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

.equals method for objects

Nitin Singla
Greenhorn

Joined: Oct 05, 2011
Posts: 15

class testToString
{
public static void main(String args[])
{
Integer i=new Integer(4);
Integer y=new Integer(4);
checkToString cs = new checkToString(2);
checkToString c = new checkToString(2);
if( cs.equals(c))
System.out.println(" Both objects are equal");
else
System.out.println(" Unequal objects ");
if( i.equals(y))
System.out.println(" i,y - Both objects are equal");
else
System.out.println(" i,y - Unequal objects ");
}
}
class checkToString
{
int i;
checkToString()
{
i=10;
}
checkToString(int a)
{
this.i=a;
}
}



In my above class, i & y are giving true on using i.equals(y) and cs & c objects are giving false on using cs.equals(c). In both the cases we are not overriding equals() method. Can someone helps me here?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

As I stated in the other thread where you asked this question, what we do in our class doesn't matter here. It's Sring's and Integers equals() methods that are being called (as we can see clearly from the code), so it's what String and Integer do with equals() that matters. And both of those classes override equals(), as you can see in their javadocs.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38517
    
  23
Jeff Verdegan wrote:As I stated in the other thread where you asked this question, . . ..
For reasons stated here, it would cause confusion if you ask the same question twice.

And welcome to the Ranch
 
Consider Paul's rocket mass heater.
 
subject: .equals method for objects