---
title: Java Program to Check Whether a String/Number is a Palindrome - Scaler Topics
description: Learn how to check palindrome string in Java by Scaler Topics. In this article, we have discussed Java program to check string palindrome.
author: Srijan Kumar Samanta
category: Java
---
:::section{.main}
A palindrome string in java is a number, word, phrase or other sequence of characters that reads the same forwards and backwards. This article focuses on determining whether a given word or number is a palindrome or not.
### Palindrome Example
The string "abcba" is a palindrome, while the string "abca" is not.
In the image given below, there is a sequence of digits that make it a palindrome number-

:::
:::section{.main}
## Methods to Check Palindrome String in Java
There are three main methods to check palindrome string in Java. They are listed below:
1. **Naive Method**
2. **Two Pointer Method**
3. **Recursive Method**
### Naive Method to Check Palindrome String in Java
In this approach, we check whether the palindrome string is in Java by reversing it and comparing it with the original string. Lets see the below example to understand clearly:
```java
import java.io.*;
class PalindromeChecker {
public static boolean isPalindrome(String str) {
// Initialize an empty string to store the reversed version of the original string
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
// Append each character to the 'reversed' string
reversed = reversed + str.charAt(i);
}
return str.equals(reversed);
}
public static void main(String[] args) {
// Example input string
String input = "radar";
// Convert the input string to lowercase (optional, for case-insensitive comparison)
input = input.toLowerCase();
boolean isPal = isPalindrome(input);
// Output the result
System.out.println("Is the string \"" + input + "\" a palindrome? " + isPal);
}
}
```
**Output:**
```
Is the string "radar" a palindrome? true
```
#### Time Complexity: O(N)
N is the length of the input string, as the for loop iterates through each character once to create the reverse string.
#### Space Complexity- O(N)
because the reverse string is stored in a separate variable proportional to the length of the input string.
### Two Pointer Method for String Palindrome Program in Java
In this approach, we utilize two pointers, `i` and `j`, where `i` points to the start of the string and `j` points to the end. We compare the characters at these positions by incrementing `i` and decrementing `j`. If they match, we continue; otherwise, we conclude that the string is not a palindrome.
```java
public class PalindromeChecker {
// Method to check if a given string is a palindrome
static boolean isPalindrome(String str) {
int left = 0, right = str.length() - 1;
while (left < right) {
if (str.charAt(left++) != str.charAt(right--))
return false;
}
return true;
}
public static void main(String[] args) {
String example1 = "radar";
String example2 = "level";
// Convert examples to lowercase
example1 = example1.toLowerCase();
example2 = example2.toLowerCase();
if (isPalindrome(example1))
System.out.println(example1 + " is a palindrome.");
else
System.out.println(example1 + " is not a palindrome.");
if (isPalindrome(example2))
System.out.println(example2 + " is a palindrome.");
else
System.out.println(example2 + " is not a palindrome.");
}
}
```
**Output:**
```
radar is a palindrome.
level is a palindrome.
```
#### Time Complexity- O(n)
#### Space Complexity- O(1)
### Recursive Method to Check String Palindrome in Java
We adopt a recursive approach similar to the two-pointer technique to determine if a palindrome string in java. Two pointers, 'i' and 'j', initially point to the start and end of the string, respectively.
We compare the characters at positions 'i' and 'j' during each recursive call. If they match, we move 'i' one step forward and 'j' one step backwards and make a recursive call with the updated indices.
This process continues until 'i' becomes greater than or equal to 'j'. If, at any point, the characters at 'i' and 'j' don't match, we immediately return false, indicating that the given string is not a palindrome.
If every character pairs match until 'i' becomes greater than or equal to 'j', we conclude that the string is a palindrome and return true.
```java
public class PalindromeChecker {
// Method to check if a given string is a palindrome
static boolean isPalindrome(String str,int i,int j) {
if (i >= j) {
return true;
}
// comparing the characters on those pointers
if (str.charAt(i) != str.charAt(j)) {
return false;
}
//rechecking everything recursively
return isPalindrome(str,i + 1, j - 1);
}
public static void main(String[] args) {
String example1 = "radar";
String example2 = "level";
// Convert examples to lowercase
example1 = example1.toLowerCase();
example2 = example2.toLowerCase();
if (isPalindrome(example1,0,example1.length()-1))
System.out.println(example1 + " is a palindrome.");
else
System.out.println(example1 + " is not a palindrome.");
if (isPalindrome(example2,0,example1.length()-1))
System.out.println(example2 + " is a palindrome.");
else
System.out.println(example2 + " is not a palindrome.");
}
}
```
**Output:**
```
radar is a palindrome.
level is a palindrome.
```
#### Time Complexity- O(n)
#### Space Complexity- O(n)
:::
:::section{.summary}
## Conclusion
* A palindrome is a word, number, phrase or a general sequence that reads the same backwards as forwards.
* We can determine whether a string is a palindrome by reversing and comparing it with the given original string.
* One slightly optimized approach is to compare the values at indices equidistant from both ends.
* We can determine a palindromic number by reversing it and comparing it with the original number.
:::