-2

The following code snippet doesn't work. Why? They are objects of the same class, and they have same value of instance variable. What is the problem?

Pen bluePen = new Pen("Faber Castel");
Pen bluePen1 = new Pen("Faber Castel");

if( bluePen.equals(bluePen1) )
    System.out.println("They are equal");

The output is empty. When I did like that:

Pen bluePen = new Pen();
Pen varPen = bluePen;

if( bluePen == varPen)
    System.out.println(“They are identical”);

There is no problem. The output is "They are identical".

I was trying to understand the difference between == and equal() method. I read the differences of these two thing in this website. But I'm confused on account of the following code snippet not to work properly:

Pen bluePen = new Pen("Faber Castel");
Pen bluePen1 = new Pen("Faber Castel");

if( bluePen.equals(bluePen1) )
    System.out.println("They are equal");

Could anyone explain to me why the code above don't output "They are equal"?

Hasan
  • 554
  • 1
  • 7
  • 19
  • duplicate of http://stackoverflow.com/questions/13387742/compare-two-objects-with-equals-and-operator – meen Jul 16 '15 at 12:35

3 Answers3

1

From this question (Why do we have to override the equals() method in Java?), you can read that the default way Java compares two objects is through their memory address. If you create two instances, both instances will have a different memory address. Hence, they are not equal. (Bottom line is that the method does work, but not as you'd expected)

That is why you have to override the equals method in your Pen class in order to 'tell' Java that if Pen.Name equals, the object itself equals.

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

How do you define "equal"?

Java doesn't know anything intuitive about your objects. It can compile and run the code, but it can't tell you which members are important when determining things like equality or sort order or anything like that. That's logic that you need to define. In the absence of that logic, the default implementation is reference equality.

In your first example, the variables refer to two different object instances. So they're not equal. In your second example, the variables refer to the same object instance. So they are equal.

In order to define equality, you need to override equals() (and hashcode()) in your class. Something like:

@Override
public int hashCode() {
    // TODO: compute and return a hash code
}

@Override
public boolean equals(Object obj) {
   // TODO: determine and return if the supplied instance is logically equal to the current instance
}

There are many good examples online of how to effectively implement these methods. Effectively the logic is simple, though. hashCode() should return a value which would be unique for any "non-equal" instance of the object, computed based on the fields used to determine equality. And equals() should determine that equality from those fields.

The overall concept becomes semantically very important when you begin to consider what objects represent in various business logic domains. For example, what makes two Person objects equal? If their names are the same? Clearly not, because two people can have the same name. A combination of their name, address, and phone number? Nope. (My brother and my father shared all of those attributes for 20 years.) What combination of attributes ensures that one Person is distinct from another Person? The answer would likely differ vastly between business domains.

Community
  • 1
  • 1
David
  • 208,112
  • 36
  • 198
  • 279
0

In Java, when the "==" operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.

The equals() method actually behaves the same as the "==" operator – meaning it checks to see if both objects reference the same place in memory. But, the equals method is actually meant to compare the contents of 2 objects, and not their location in memory.

. You can even override the equals() method to define yourself when an object is equal to another

Rohit
  • 895
  • 1
  • 9
  • 19