https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Java-palindrome-program-for-Strings-example
Good programmers need to create code that efficiently solves problems, using various methods. A popular academic exercise is to create a program that determines if a number or String is a palindrome.
To write a Java String palindrome program, follow these steps:
Here is an example of a recursive Java palindrome program that checks String literals:
package com.mcnz.palindrome.example; public class JavaStringPalindromeProgram { /* The main method declares three Strings for the Java palindrome program to check. */ public static void main(String[] args) { boolean flag = javaPalindromeCheck("sitonapanotis"); System.out.println(flag); flag = javaPalindromeCheck("nine"); System.out.println(flag); flag = javaPalindromeCheck("amanaplanacanalpanama"); System.out.println(flag); } /* This method does a recursive Java palindrome check */ public static boolean javaPalindromeCheck(String s){ if(s.length() == 0 || s.length() == 1) { return true; } if(s.charAt(0) == s.charAt(s.length()-1)) { return palindromeCheck(s.substring(1, s.length()-1)); } return false; } /* End of Java String palindrome checker method */ } /* End of Java palindrome program */
When this program runs, it returns:
Here are 10 other text Strings for which the Java palindrome checker program should return a true value. Put these into your code and test them:
With these examples, your Java palindrome program must decide whether to ignore non-text characters and punctuation, which will add to the complexity of the program.
This Java palindrome example uses recursion, which is an advanced concept.
There are also drawbacks to recursion, as it can place a heavy load on the Java stack and eventually trigger a StackOverflowError and terminate your program
Other approaches to solve the Java String palindrome problem include the use of loops and arrays.
Test your skills and see if you can implement your own Java palindrome program with loops, arrays and maybe even the Scanner class to dynamically generate input from the user.
Cameron McKenzie is an AWS Certified AI Practitioner, Machine Learning Engineer, Solutions Architect and author of many popular books in the software development and Cloud Computing space. His growing YouTube channel training devs in Java, Spring, AI and ML has well over 30,000 subscribers.
11 Jun 2025