Palindrome using StringBuffer

This is my program:

public class Palindrome

{

public static void main(String[] args)

{

String str1= new String("malayalam");

StringBuffer str2= new StringBuffer(str1);

StringBuffer str3= new StringBuffer(str1);

str2.reverse();

System.out.println("First String:"+str2+ "Length:"+str2.length());

System.out.println("Second String:"+str3+ "Length:"+str3.length());

if (str2.equals(str3))

System.out.println("Was a palindrome");

else

System.out.println("Was not a palindrome");

}

}

The output I'm getting is:

First String:malayalamLength:9

Second String:malayalamLength:9

Was not a palindrome

Where is the problem?

[772 byte] By [deepanjan_naga] at [2007-11-26 15:54:34]
# 1

The problem is here:

str2.equals(str3)

Class StringBuffer hasn't its own method equals(), so it uses the equals() of class Object which it inherites.

And as we know "Object.equals()" returns true only when two references have the same object.

stoppera at 2007-7-8 22:15:06 > top of Java-index,Java Essentials,New To Java...
# 2
You want to compare strings, not StringBuffers. Useif ((str2.toString()).equals(str3.toString()))too late.Message was edited by: ChuckBing
ChuckBinga at 2007-7-8 22:15:06 > top of Java-index,Java Essentials,New To Java...