0
public class Counter {

    String s1;

    Counter(String s) {
        this.s1 = s;
    }

    public static void main(String[] args) {
        Counter t1 = new Counter("test");
        Counter t2 = new Counter("test");
        System.out.println(t1 == t2); //false - Output
        System.out.println(t1.equals(t2)); //false - Output

        String s1 = new String("rt");
        String s2 = new String("rt");
        System.out.println(s1 == s2); //false - Output
        System.out.println(s1.equals(s2)); //true - Output
    }
}

Reason: Getting different output for equals method.

Since java.lang.String class override equals method, It return true if two String object contains same content but == will only return true if two references are pointing to same object.

For == operator I am getting proper result but for equals method I am bit confused in my program as the output is different. Please, can anyone share their views on this? thanks in advance

Harmlezz
  • 7,972
  • 27
  • 35

5 Answers5

0

String class overrides equals but your Counter class does not, which is why equals behaves the same as ==. t1.s1.equals(t2.s1) would return true.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Your class Counter does not override the equals method so it behaves same as == . However in String class this method is overriden to return true if the contents of the Strings match. You can override the method in your class to achieve the same functionality

Pooja Arora
  • 574
  • 7
  • 19
0

equals() method of your String class is defined as:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = value.length;
        if (n == anotherString.value.length) {
            char v1[] = value;
            char v2[] = anotherString.value;
            int i = 0;
            while (n-- != 0) {
                if (v1[i] != v2[i])
                    return false;
                i++;
            }
            return true;
        }
    }
    return false;
}

whereas equals() method for your Counter class is defined like this (inherited for Object class)

public boolean equals(Object obj) {
    return (this == obj);
}

as you can see, both of these methods does different things and you get different result. If you want same result, then you can override equals() method in your Counter class to implement String like functionality, like this:

public class Counter {

    String s1;

    Counter(String s) {
        this.s1 = s;
    }

    @Override
    public boolean equals(Counter counter) {
        return this.s1.equals(counter.s1);
    }

    ...
    ...
}
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
0

== and equals is not the same, == is a operation that can compare memory address of objects while subject to Objects. but equal method just return a boolean that depends on the equal method what contain inside. equal method is located on Object class. so you can override the method and you can say its equal or not.

in your case for example

class Counter {
       private int count;
       public boolean equals(Object another){
           return another instanceof Counter && ((Counter)another).count == this.count;
       }
}

hence you override the equal method, you need to override the hashcode method, if you are going to deal with HashMap and your object as key. not only the HashMap, where ever the hashing technique involved, here is the reason why. http://tutorials.jenkov.com/java-collections/hashcode-equals.html

subash
  • 3,116
  • 3
  • 18
  • 22
0

Your class Counter does neither override hashCode() nor equals(). Hence your class Counter behave exactly as the class Object does. Two Objects are == and equals() only if they represent the same instance.

If you like to change this behavior, you have to override equals() and hashCode() of Counter accordingly to the contract as defined by the equals() method.

Harmlezz
  • 7,972
  • 27
  • 35