-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?

Ann Zen
  • 25,080
  • 7
  • 31
  • 51
Thatdude22
  • 57
  • 7

1 Answers1

1

You have to use equals instead of ==.

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

Ann Zen
  • 25,080
  • 7
  • 31
  • 51
Oleh Kurpiak
  • 1,206
  • 12
  • 27