-1

I'm comparing two objects with the same attributes and values yet it's returning false

I divided the two if's conditions in order to check which one was wrong. I have debugged the algorithm so I could check if there was something wrong with my object's attribute values.

https://gyazo.com/73427a2a20558901019fc885745ca1d2

 public boolean existsBag(Bag bag) {
        for (int i = 0; i < this.insertedBags.length; i++) {
            if (this.insertedBags[i] != null ) {
                if(this.insertedBags[i].equals(bag)){
                    return true;
                }

            }
        }
        return false;
    }

UPDATE

Problem solved by overinding the equals method in the Bag Class, thanks guys!

  • 5
    Does the `Bag` class implement `equals`? If not, then `bag1.equals(bag2)` is the same as `bag1 == bag2` – Andreas Apr 22 '19 at 16:31
  • 1
    By default, equals behaves like == (i.e., true only for the same object). – floxbr Apr 22 '19 at 16:32
  • even if bags have same attributes and values, they are different objects. Agree with @floxbr. – Talgat Apr 22 '19 at 16:34
  • As an aside, you could use a short circuit operator here instead of nested if's `this.insertedBags[i] != null && this.insertedBags[i].equals(bag)` – markspace Apr 22 '19 at 16:39
  • override equals() method to check equality based on the objects attributes that makes sense. – shakhawat Apr 22 '19 at 16:39
  • @markspace i stated that they were separated willingly in order to bea ble to see what was working and what not – Eduardo Eiroa Ballester Apr 22 '19 at 16:41
  • 1
    As Progman's link points out, if you override `equals` you almost always should also override `hashcode`. It's a good habit to get into. – markspace Apr 22 '19 at 16:46

6 Answers6

1

Need to override overrides equals() method if you are comparing objects otherwise it will return false.

Here is an example how to override equals method.

Consider an Employee class which has two fields: age and name.

public class Employee {

String name;
int age;

public Employee(String name, int age) {
    this.name = name;
    this.age = age;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

@Override
public boolean equals(Object obj) {
    if (obj == this)
        return true;
    if (!(obj instanceof Employee))
        return false;
    Employee employee = (Employee) obj;
    return employee.getAge() == this.getAge()
            && employee.getName() == this.getName();
}
}
Anto Livish A
  • 282
  • 3
  • 15
1

As mentioned in the comments you are likely not to have overridden the equals method.

Just an hint to what it may look like

public class Bag {

    @Override
    public boolean equals(final Object other) {

        if (this == other)
            return true;

        if ( ! (other instance of Bag))
            return false;

        // now compare your inner fields. E.g:
        final Bag bagOther = (Bag) other;

        if (! this.brandName.equals(bagOther.brandName)) // brandName is java.lang.String
            return false;
        if (! this.length == bagOther.length) // length is int
            return false;

         // ...
         return true;
    }
}

If not overridden, equals by default uses its base class Object's version which just compares for the instances memory location.

Also note that besides overriding equals you should also override the method hashCode.

E.g.: this article that explains how and why

Marko Pacak
  • 3,381
  • 1
  • 9
  • 37
0

Just override methods equals() and hashCode() in Bag class.

The first one is used to compare the object values and the second one is used to create the object hash inside the JVM. This way you'll get the correct comparison.

R. Karlus
  • 1,568
  • 1
  • 16
  • 38
0

By default all classes inherit the Object class in java. As such, the Object class, and hence all other classes have a public boolean equals(Object o) method which simply returns this == o.

[This is true for OpenJDK, and is probably same for Oracle JDK as well.]

To specify your custom equality logic, you must override this public boolean equals(Object) method with your own comparison logic. Other answers have already provided relevant code. For more discussion refer to this Stackoverflow thread.

pmcarpan
  • 620
  • 6
  • 12
0

By default equals compare the reference of the 2 objects, so you need to override equals() method if you are comparing objects otherwise it will return false.

idan
  • 638
  • 1
  • 11
  • 28
MAJID
  • 1
-1

According to api:

for any non-null reference values x and y, equals returns true if and only if x and y refer to the same object (x == y has the value true).

Sabareesh
  • 604
  • 5
  • 13