Java Tutorial
🔍
Java ProgramsStringsCheck Palindrome String

Check Palindrome String in Java

beginner·  Strings  ·  String

Problem

A palindrome is a string that reads the same forwards and backwards.

Given a string, determine whether it is a palindrome.

Input
madam
Output
madam is palindrome: true

Java Program

Java
public class PalindromeCheck { public static void main(String[] args) { String str = "madam"; // Build the reversed version of the string String reversed = new StringBuilder(str).reverse().toString(); // A palindrome reads the same forwards and backwards boolean isPalindrome = str.equals(reversed); System.out.println(str + " is palindrome: " + isPalindrome); } }

Output

madam is palindrome: true

Core Logic

The simplest check is to reverse the string and see if it matches the original — StringBuilder makes that a one-liner.

How It Works
  1. 1The original string is passed to new StringBuilder(str), which wraps it in a mutable buffer.
  2. 2.reverse() flips the character order in place, and .toString() converts the buffer back into a String.
  3. 3str.equals(reversed) compares the two strings character by character, not by reference.
  4. 4If every character matches its mirrored position, equals() returns true and the string is confirmed a palindrome.
For "madam", reversing it also produces "madam", so equals() returns true.
💡

Key Point: Always compare with .equals(), not ==== checks object reference, not character content, and would give the wrong answer here.

Key Concepts

StringBuilderreverse()equals()

Approach 2: Manual Two-Pointer Comparison

Java
public class PalindromeCheckManual { public static void main(String[] args) { String str = "madam"; boolean isPalindrome = true; int left = 0, right = str.length() - 1; // Move left and right toward each other, comparing mirrored characters while (left < right) { if (str.charAt(left) != str.charAt(right)) { isPalindrome = false; break; // mismatch found, no need to keep checking } left++; right--; } System.out.println(str + " is palindrome: " + isPalindrome); } }

Output

madam is palindrome: true

Core Logic

No built-in reverse this time — just two pointers closing in from opposite ends, comparing characters as they go.

How It Works
  1. 1Two index variables, left and right, start at the first and last character positions.
  2. 2On each iteration, charAt(left) and charAt(right) are compared — if they differ, the string cannot be a palindrome, so isPalindrome is set to false and the loop exits early with break.
  3. 3If the characters match, left moves inward and right moves inward, narrowing the comparison window.
  4. 4The loop stops once left meets or crosses right, meaning every pair has been checked.
For "madam", the pairs checked are (m,m) and (a,a); the loop stops once left and right meet at the middle 'd' — no mismatch was found, so isPalindrome stays true.
💡

Key Point: This runs in the same O(n) time as the StringBuilder version, but uses O(1) extra space instead of O(n), since it never builds a second, reversed copy of the string — a detail interviewers often ask about directly.

Key Concepts

two-pointer techniquecharAt()early exit with break

Approach 3: Recursive Check

Java
public class PalindromeCheckRecursive { static boolean isPalindrome(String str, int left, int right) { // Base case: pointers met or crossed, every pair matched if (left >= right) return true; // Mismatch found, not a palindrome if (str.charAt(left) != str.charAt(right)) return false; // Recurse inward, shrinking the comparison window return isPalindrome(str, left + 1, right - 1); } public static void main(String[] args) { String str = "madam"; boolean isPalindrome = isPalindrome(str, 0, str.length() - 1); System.out.println(str + " is palindrome: " + isPalindrome); } }

Output

madam is palindrome: true

Core Logic

The same two-pointer idea also works recursively: each call checks one pair of characters and hands off a slightly smaller window to the next call.

How It Works
  1. 1The base case if (left >= right) return true; fires once the pointers meet or cross, meaning every pair has matched.
  2. 2Each call compares str.charAt(left) and str.charAt(right) — a mismatch immediately returns false, short-circuiting the recursion.
  3. 3If the characters match, the call recurses inward with isPalindrome(str, left + 1, right - 1).
  4. 4The recursion naturally stops as soon as either a mismatch is found or the pointers meet.
For "madam", the calls compare (m,m) then (a,a), then hit the base case at the middle 'd', returning true all the way back up.
💡

Key Point: Functionally identical to the iterative two-pointer version, but each recursive call adds a stack frame — for very long strings, the loop-based version avoids that overhead.

Key Concepts

recursionbase casetwo-pointer technique

Related Programs