2 min read

How to Check if a String is a Palindrome in Java

Introduction

In this short post we'll discover several techniques on how to check if a given String is a palindrome in Java.

This is another task that is frequently asked on Java programming interviews from time to time.

What is a palindrome?

By definition, a [palindrome](Palindrome - Wikipedia) word is read the same backward as forward. For example: radar or racecar.

Solutions

Using Loops

Let's start with the simplest solution to the palindrome problem:

public static boolean isPalindrome(String input) {
    int start = 0;
    int end = input.length() - 1;
    while (start < end) {
        if (input.charAt(start) != input.charAt(end)) {
            return false;
        }
        start++;
        end--;
    }

    return true;
}

The simplest solution

In this approach we compare the characters at positions pointed by the variables start and end in a loop. In each iteration we increase start and decrease end until they become equal.

In case the characters are different, the function immediately returns false meaning the given string is not a palindrome.

On the other hand, if begin becomes equal to end then we the provided string is a palindrome.

(Although it's out of scope, but it's also worth adding a check in the beginning of the function to make sure it's neither empty or null string.)

One Liner using StringBuilder

A very elegant and compact solution to check if the provided input is palindrome is to use the StringBuffer class of Java:

public static boolean isPalindromeStringBuilder(String input) {
    return input.equals(new StringBuffer(input).reverse().toString());
}

The same method was used in the previous article when we looked at how we can reverse a string.

One liner solution using StringBuilder

Recursive Approach

If you are asked to provide a solution without loops and StringBuilder, then you might end up writing a recursive method:

public static boolean isPalindromeRecursive(String input) {
    if (input.length() < 2) {
        return true;
    }
    return input.charAt(0) != input.charAt(input.length() - 1) ? false :
            isPalindromeRecursive(input.substring(1, input.length() - 1));
}

In this method we continously shrink the string by using the substring function.

For example, this is how the string will look like in the subsequent iterations:

1.  radar
2.  ada
3.  d

The default stop condition to flag the string is not a palindrome is checking whether the first and the last characters of the shrinked substring are the same. If they aren't, the string is not a palindrome.

It's important to take care of the other stop condition in case of a palindrome string — this is what the first if condition is responsible for. Otherwise, it'd continue shrinking the string endlessly (and eventually resulting in StackOverflowError error after a certain amount of iterations).

Using recursive solution

Summary

In this post we've seen three different ways of checking whether a string is a palindrome.

Just like reversing a string, this task is not a very common one in production code either. However, if you are just getting familiar with the Java language or will have a Java interview for a junior position, then it's really useful to understand and practice this task.