View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Java Program for String Palindrome

By Pavan Vadapalli

Updated on Mar 28, 2025 | 4 min read | 5.7k 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:

  • Assign the number to a temporary variable
  • Obtain the reverse of the number
  • Compare the value of the number in the temporary variable with the reversed value
  • When there is no difference between the two values, print a message stating that it is a palindrome. Otherwise, print a message stating that the number is not a palindrome.

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!

How to Check String Palindrome in Java

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:

  • Reversing the string using the for loop
  • The loop operates from the beginning to the end of the string.
  • The charAt() method is useful for accessing every string character.
  • Every character is stored in reverseStr after being accessed in reverse order. 
  • If statement to compare str and reverseStr

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. 

How to Check String Palindrome Program in Java

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:

  • While loop to reverse num and preserve the reversed number in reversedNum
  • To check if reversedNum is the same as the originalNum, the if…else has been used

How to Write the Palindrome Number Program in Java

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months
View Program

Job-Linked Program

Bootcamp36 Weeks
View Program

To write the palindrome number program in Java, the following algorithm will be followed:

  • Use the number to look for palindromes.
  • Keep the number in a temporary variable.
  • Reverse the number.
  • Use the reversed number to compare it with the temporary number.
  • When both numbers are the same, you can print the palindrome number.
  • If both numbers are not the same, you will have to print not a palindrome number. 

Frequently Asked Questions (FAQs)

1. What is the palindrome Java program?

2. Can you call 121 a palindrome number in Java?

3. Which is the highest palindrome in Java?

Pavan Vadapalli

899 articles published

Get Free Consultation

+91

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

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

View Program
upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

View Program
upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

View Program