I wrote this method to check if the given string is a palindrome or not and depending on the result I implemented an if-else statement to print out something. I know the string is a palindrome but the result I am getting is different from what I expected. Here is the java code:
public class Palindrom {
public static void main(String[] args){
String palindrome = "DOT SAW I WAS TOD";
String result = "";
for(int i = palindrome.length() - 1; i > 0; i--){
result = result + palindrome.charAt(i);
}
if (result == palindrome){
System.out.println("Yes it is a palindrome");
} else {
System.out.println("No it is not palindrome");
}
}
} //No it is not palindrome
What I suspected the problem is on result ==
palindrome. Is this getting false because I am comparing two different objects? What method should I use to solve such problems?