Java Program for String Palindrome
Updated on Mar 28, 2025 | 4 min read | 6.17K+ views
Share:
For working professionals
For fresh graduates
More
Updated on Mar 28, 2025 | 4 min read | 6.17K+ views
Share:
When a number remains the same even after reversal, it is referred to as a palindrome. Some examples of palindromes include 656, 232, 46764, and the like. Palindromes can also exist as strings such as MADAM (Palindrome Program in Java , n.d.). In Java, a basic algorithm may be applied to check whether a number is a palindrome. The basic steps included in the algorithm are summarized below:
Check out our free technology courses to get an edge over the competition.
Several other approaches may be applied to check whether a given number or string is a palindrome. In the example below, two pointers are used to navigate from the beginning to the end of the input provided. The program confirms whether the supplied input is a palindrome (Java program to check whether a string is a Palindrome, 2019).
Learn Software Development Courses online from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs or Masters Programs to fast-track your career.
Amid increased conversations around crypto and Blockchain technology, if you wish to educate yourself professionally on the topic, then upGrad’s Executive Post Graduate Programme in Software Development – Specialisation in Blockchain under IIIT- Bangalore is the right choice for you!
The code to check string palindrome in Java is as follows:
class Main {
public static void main(String[] args) {
String str = "Radar", reverseStr = "";
int strLength = str.length();
for (int i = (strLength - 1); i >=0; --i) {
reverseStr = reverseStr + str.charAt(i);
}
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
System.out.println(str + " is a Palindrome String.");
}
else {
System.out.println(str + " is not a Palindrome String.");
}
}
}
Output:
Radar is a Palindrome String.
In this example, the “Radar” string has been stored in str. The following has been used for the code:
The toLowerCase() method can transform both reverseStr and str to lowercase. It happens because Java is extremely case-sensitive. Therefore, R and r are two different values in Java.
The equals() method can determine whether there’s equality between two strings.
The code to check the string palindrome program in Java is as follows:
class Main {
public static void main(String[] args) {
int num = 3553, reversedNum = 0, remainder;
// store the number to originalNum
int originalNum = num;
// get the reverse of originalNum
// estore it in variable
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
// check if reversedNum and originalNum are equal
if (originalNum == reversedNum) {
System.out.println(originalNum + " is Palindrome.");
}
else {
System.out.println(originalNum + " is not Palindrome.");
}
}
}
Output:
3553 is Palindrome.
In this example, the number 3533 is stored in num and originalNum variables. The following has been used here:
To write the palindrome number program in Java, the following algorithm will be followed:
A palindrome string in Java remains the same when it is reversed. Some examples of palindromes include level and radar. Similarly, any number equal to the reverse of it is referred to as a palindrome number. A few examples of palindrome numbers include 1232 and 3533.
121 can be called a palindrome because it remains the same when it is reversed. But 123 is not a palindrome because it gets changed when it is reversed.
The highest palindrome in any Java array is 1238321.
900 articles published
Pavan Vadapalli is the Director of Engineering , bringing over 18 years of experience in software engineering, technology leadership, and startup innovation. Holding a B.Tech and an MBA from the India...
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources