0

I had made a program on .equals() in java & having some conceptual problem when I saw some of the online videos on youtube & searched about this thing but not got proper explanation. So guys help me with that thing.

Thanks.

package Practice;

public class StringManipulation11 {

    StringManipulation11(String s) {
    }

    public static void main(String[] args) {

        String s = "Good";
        String s1 = "Good";
        String s2 = "Morning";

        String t = new String("Good");
        String t1 = new String("Good");
        String t2 = new String("Morning");

        StringManipulation11 sm = new StringManipulation11("Good");
        StringManipulation11 sm1 = new StringManipulation11("Good");

        System.out.println(s.equals(s1));// true because check content
        System.out.println(s.equals(s2));// false content not match

        System.out.println(t.equals(t1));// true because check content
        System.out.println(s.equals(t));// true because check content

        System.out.println(sm.equals(sm1));// false, but not getting the reason
                                            // why it is false

        /*
         * In this case also the content is same but not getting the proper
         * conclusion why it is false & it is false then why i am getting true
         * in "System.out.println(t.equals(t1))" in this condtion.
         */

        System.out.println(s.equals(sm));

    }
}
Nurjan
  • 5,532
  • 5
  • 32
  • 50
Ash
  • 21
  • 3

4 Answers4

4

In this case, also the content is same but not getting the proper conclusion why it is false & it is false then why I am getting true in System.out.println(t.equals(t1)) in this condition.

The class String has an implementation of equals (and hashcode) which compares the two objects character by character. Your class does not have an implementation of these methods, so it uses the implementation it has inherited from Object, which compares the references, i.e. for it to be true, the instances need to be the same.

That's an important distinction to get your head around, same means these two references are pointing to the exact same instance. And equals means that they are either the same or have equivalent content, but it is up to you to define how the content is compared, see this.

Community
  • 1
  • 1
weston
  • 52,585
  • 20
  • 135
  • 197
0

StringManipulation11 is extend object, if you didn't override the equals method the default method is

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

you can compare the equals method in String

public boolean equals(Object var1) {
        if(this == var1) {
            return true;
        } else {
            if(var1 instanceof String) {
                String var2 = (String)var1;
                int var3 = this.value.length;
                if(var3 == var2.value.length) {
                    char[] var4 = this.value;
                    char[] var5 = var2.value;

                    for(int var6 = 0; var3-- != 0; ++var6) {
                        if(var4[var6] != var5[var6]) {
                            return false;
                        }
                    }

                    return true;
                }
            }

            return false;
        }
    }
chaoluo
  • 2,426
  • 1
  • 15
  • 27
0

This is the definition of the equals method in the String class (Java 8):

public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only
if the argument is not null and is a String object that represents the same
sequence of characters as this object.

Which means that this method returns true if and only if both Strings represent the same sequence of characters.

In your case you have defined a new class StringManipulation11 which by default uses the equals method of the Object class. And this is its definition:

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).

In your example you have defined two different objects sm and sm1. These variables refer to DIFFERENT objects and this is why the equals method returns false.

Nurjan
  • 5,532
  • 5
  • 32
  • 50
0

If you want to check equality with equals() method in your own written class you have to override equals() method of Object class.The default implementation of equal() inherited from Object, returns true if and only if the two variables point to the same object.While in your case sm and sm1 object contain the same information but they are different objects in memory.That's why its returning false.

Zia
  • 1,122
  • 11
  • 24
  • String t = new String("Good"); String t1 = new String("Good"); This is also the different objects then why it is giving true. – Ash Dec 13 '16 at 06:46
  • Whenever you are creating your own objects and you want to check equality of that object you should override (and implement) this equals method in your class.and in String class same way the Java language provides this equality/comparison behavior in the String equals method.Because of the implemented/overriden equals method in String class you are getting this as true – Zia Dec 13 '16 at 06:56