Java Program to Check Whether a String is a Palindrome

Last Updated : 12 May, 2026

A palindrome string is a string that reads the same forward and backward. In Java, palindrome programs are commonly used to practice string handling, loops, and conditional statements.

Illustration

Input: s = "level"
Output: True

Input: s = "Geeks"
Output: False

Input s = "abc"
Output: False.

There are multiple approaches to check whether a string is a palindrome or not. Let's discuss one by one.

1. Brute Force Approach

The simplest way to check a palindrome is to reverse the string and compare it with the original string. To handle case-insensitive comparisons, we convert the string to lowercase.

Algorithm

  • Convert the string to lowercase
  • Reverse the string using a loop
  • Compare original string and reversed string
  • If both are equal -> palindrome, else not palindrome
Java
public class GFG{

    public static boolean isPalindrome(String s){

        // Convert to lowercase for case-insensitive check
        s = s.toLowerCase();

        // Reverse the string
        String rev = "";
        for (int i = s.length() - 1; i >= 0; i--) {
            rev = rev + s.charAt(i);
        }

        // Compare original and reversed
        return s.equals(rev);
    }

    public static void main(String[] args) {

        String s = "level";

        if (isPalindrome(s)) {
            System.out.println("\"" + s + "\" is a palindrome.");
        } else {
            System.out.println("\"" + s + "\" is not a palindrome.");
        }
    }
}

Output
"level" is a palindrome.

Explanation: In the above example, It checks if the given string is palindrome by converting it to lowercase, reversing it, and then comparing the reversed string with the original.

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2. Two Pointer Approach

This approach uses two pointers, one starting at the beginning "i" and the other at the end "j" of the string. By comparing characters at these pointers and moving them inward, we can determine if the string is a palindrome or not.

Algorithm

  • We initialize two pointers (i=0) from the start and (j = s.length -1) from the end.
  • Compare the characters at index i and j, if both are not equal then return false( it is not a palindrome).
  • If they match then increment i by 1 and decrement j by 1 and continue the process.
  • If all the characters match and the loop stops at ( i = j), then it is a palindrome and returns true.
Java
public class GFG{

    public static boolean isPalindrome(String s){

        s = s.toLowerCase();
        int i = 0, j = s.length() - 1;

        while (i < j) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    public static void main(String[] args){

        String s1 = "geeks";
        String s2 = "Racecar";

        System.out.println("\"" + s1 + "\" palindrome? " + isPalindrome(s1));
        System.out.println("\"" + s2 + "\" palindrome? " + isPalindrome(s2));
    }
}

Output
"geeks" palindrome? false
"Racecar" palindrome? true

Explanation: In this example, the isPalindrome method checks for mismatched characters while i < j and returns false if found. The main method converts input strings to lowercase and prints whether they are palindrome.

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3. Recursive Approach

In recursion, we use a two-pointer approach similar to iteration. We take one pointer i from the start and another pointer j from the end of the string. At each recursive step, we compare characters at index i and j.

Algorithm

  • Take two pointers: i = 0 (start) and j = n-1 (end)
  • Base Case: If i >= j, return true (palindrome confirmed)
  • If s.charAt(i) != s.charAt(j), return false
  • Otherwise, call recursion for next characters: isPalindrome(i+1, j-1)
  • If all pairs match, the string is a palindrome
Java
public class GFG{

    public static boolean isPalindrome(String s, int i, int j) {

        if (i >= j) return true;

        if (s.charAt(i) != s.charAt(j)) return false;

        return isPalindrome(s, i + 1, j - 1);
    }

    public static void main(String[] args) {

        String s = "Racecar";
        s = s.toLowerCase();

        System.out.println(isPalindrome(s, 0, s.length() - 1));
    }
}

Output
true

Explanation:In this example, the isPalindrome method uses recursion with two pointers (i and j) to compare characters from the start and end. If a mismatch is found, it returns false; otherwise, it continues checking inward until the base condition (i >= j) is reached. The main method converts the string to lowercase and prints whether it is a palindrome.

  • Time Complexity: O(n)
  • Space Complexity: O(n)
Comment