-1

Here is my question. It's asking to return true if int x is a palindrome otherwise return false:

public class Palindrome {

    public boolean isPalindrome(int x) {

        StringBuilder number = new StringBuilder(Integer.toString(x));

        return (number.reverse() == number) ? true : false;
    }

    public static void main(String[] args) {
        Palindrome object = new Palindrome();
        boolean state = object.isPalindrome(45678);
        System.out.println(state);

    }

}

I think my logic makes perfect sense here. If the reverse of the number is equal to the original number , return true (121 = 121). How is 87654 = 45678? Can you explain why my method doesn't work?

Red
  • 26,798
  • 7
  • 36
  • 58
Thatdude22
  • 57
  • 9

1 Answers1

1

You have to use equals instead of ==.

Check this answer Compare two objects with .equals() and == operator

Red
  • 26,798
  • 7
  • 36
  • 58
Oleh Kurpiak
  • 1,339
  • 14
  • 34