-1

public class reverse_string { public static void main(String[] args) {

    StringBuffer s1 =  new StringBuffer("madam");
    System.out.print(s1);
    StringBuffer s2 = new StringBuffer(s1.reverse());
    System.out.print("\n"+s2);


    if(s1.equals(s2))
    {
        System.out.print("\nreal string and reverse string are matched");
    }
    else
    {
        System.out.print("\nreal string and reverse string are not matched");
    }

} }

disha
  • 3
  • 5

3 Answers3

0

StringBuffer will do a reference comparison with equals. Since they are not the same object, the comparison returns false.

change your code to

s1.toString().equals(s2.toString())

this will compare the values of the Strings.

Also: JavaScript and Java are not the same thing.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
0

You are comparing two StringBuffer instantes, not the strings itself.

Try adding .toString() to convert them to a String.

tiagodws
  • 1,345
  • 13
  • 20
0

StringBuffer doesn't override equals() from String, its equals() method is the one inherited from java.lang.Object, which returns true if and only if "==" would be true. That is, it compares the references of the two objects, which are not equal.