0
0
JavaProgramBeginner · 2 min read

Java Program to Check Palindrome String

You can check a palindrome string in Java by comparing characters from start and end using a loop, for example: boolean isPalindrome = true; for (int i = 0; i < str.length()/2; i++) { if (str.charAt(i) != str.charAt(str.length() - 1 - i)) { isPalindrome = false; break; } }.
📋

Examples

Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
Inputa
Outputa is a palindrome
🧠

How to Think About It

To check if a string is a palindrome, think of reading it from both ends at the same time. Compare the first character with the last, the second with the second last, and so on. If all pairs match, the string is a palindrome; if any pair differs, it is not.
📐

Algorithm

1
Get the input string.
2
Start from the first character and the last character.
3
Compare these two characters.
4
If they are different, return that the string is not a palindrome.
5
If they are the same, move inward and repeat until the middle is reached.
6
If all pairs match, return that the string is a palindrome.
💻

Code

java
public class PalindromeCheck {
    public static void main(String[] args) {
        String str = "madam";
        boolean isPalindrome = true;
        for (int i = 0; i < str.length() / 2; i++) {
            if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
                isPalindrome = false;
                break;
            }
        }
        if (isPalindrome) {
            System.out.println(str + " is a palindrome");
        } else {
            System.out.println(str + " is not a palindrome");
        }
    }
}
Output
madam is a palindrome
🔍

Dry Run

Let's trace the string "madam" through the code to see how it checks for palindrome.

1

Initialize variables

str = "madam", isPalindrome = true

2

First iteration (i=0)

Compare str.charAt(0) = 'm' and str.charAt(4) = 'm' -> equal

3

Second iteration (i=1)

Compare str.charAt(1) = 'a' and str.charAt(3) = 'a' -> equal

4

Loop ends

All compared characters matched, isPalindrome remains true

5

Print result

Output: "madam is a palindrome"

istr.charAt(i)str.charAt(str.length()-1-i)Match?
0mmYes
1aaYes
💡

Why This Works

Step 1: Compare characters from ends

The code compares characters starting from the first and last positions moving inward using charAt.

Step 2: Stop if mismatch found

If any pair of characters differ, the loop breaks early and sets isPalindrome to false.

Step 3: Confirm palindrome

If all pairs match, the string is confirmed as a palindrome and the program prints the positive result.

🔄

Alternative Approaches

Using StringBuilder reverse method
java
public class PalindromeCheck {
    public static void main(String[] args) {
        String str = "madam";
        String reversed = new StringBuilder(str).reverse().toString();
        if (str.equals(reversed)) {
            System.out.println(str + " is a palindrome");
        } else {
            System.out.println(str + " is not a palindrome");
        }
    }
}
This method is simpler but uses extra memory to create a reversed string.
Recursive palindrome check
java
public class PalindromeCheck {
    public static boolean isPalindrome(String str, int start, int end) {
        if (start >= end) return true;
        if (str.charAt(start) != str.charAt(end)) return false;
        return isPalindrome(str, start + 1, end - 1);
    }
    public static void main(String[] args) {
        String str = "madam";
        if (isPalindrome(str, 0, str.length() - 1)) {
            System.out.println(str + " is a palindrome");
        } else {
            System.out.println(str + " is not a palindrome");
        }
    }
}
This recursive method is elegant but may use more stack space for long strings.

Complexity: O(n) time, O(1) space

Time Complexity

The program checks up to half the string length, so it runs in linear time O(n), where n is the string length.

Space Complexity

It uses only a few variables and no extra data structures, so space complexity is O(1).

Which Approach is Fastest?

The loop comparison method is fastest and uses least memory compared to reversing the string or recursion.

ApproachTimeSpaceBest For
Loop character comparisonO(n)O(1)Memory efficient and fast
StringBuilder reverseO(n)O(n)Simple code but uses extra memory
Recursive checkO(n)O(n)Elegant but uses stack space
💡
Always compare characters from both ends moving toward the center to efficiently check palindrome.
⚠️
Beginners often forget to stop the loop at half the string length, causing unnecessary checks or errors.