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,889
  • 5
  • 34
  • 54
Ash
  • 21
  • 3

3 Answers3

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
  • 54,145
  • 21
  • 145
  • 203
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,596
  • 1
  • 17
  • 29
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,889
  • 5
  • 34
  • 54