1. What is a Palindrome?
A palindrome is a word, phrase, number, or any other sequence of units that may be read the same way in either direction. Generally, if found, commas, separators or other word dividers are ignored, but it is not a mandatory rule.
Similarly, palindrome numbers are those numbers that represent the same number if all digits are reversed (Underscores can be ignored in large numbers such as 1_00_00_001). Underscores in numeric literals are the new addition to Java 7 features.
2. Algorithm
The general steps to check if the specified string or number is a palindrome, follow these steps:
- Store the original string in a variable.
- Reverse the String and store it in another variable.
- Compare both variables.
- If both strings are equal, then the string is a palindrome.
- If both strings are unequal, then the string is not a palindrome.
3. Java Palindrome Programs
To check palindrome string, reverse the String characters. Now use equals() or equalsIgnoreCase() method to verify if the given string was palindrome or not.
3.1. By Reversing the String
The StringBuilder.reverse() and apache Common Lang’s StringUtils.reverse() methods can reverse the method in a single line.
public static boolean isPalindromeUsingStringBuilder(String originalString) {
String reverse = new StringBuilder(originalString).reverse().toString();
return originalString.equals(reverse);
}
public static boolean isPalindromeUsingStringUtils(String originalString) {
String reverse = StringUtils.reverse(originalString);
return originalString.equals(reverse);
}
3.2. Using For-Loop
Use for loop to get the reverse string by iterating over string characters from the last index using charAt() method and create a new string.
Use this approach only when we are checking string palindrome without using inbuilt reverse methods.
public static boolean isPalindromeUsingForLoop(String originalString) {
String reverse = "";
for (int i = originalString.length() - 1; i >= 0; i--) {
reverse = reverse + originalString.charAt(i);
}
return originalString.equals(reverse);
}
3.3 Using Recursion
In recursion, rather than creating a new String, which is the reverse of the original String, we can compare the characters of the given string from the beginning and the end.
If the characters at the corresponding positions from beginning and end are the same, then the string is a palindrome.
public static boolean isPalindromeUsingRecursion(String string) {
if (string.length() == 0 || string.length() == 1) {
return true;
}
if (string.charAt(0) == string.charAt(string.length() - 1)) {
return isPalindromeUsingRecursion(string.substring(1, string.length() - 1));
}
return false;
}
4. Demo
Let us run all the above-discussed approaches and test their output.
String input1 = "howtodoinjava";
String input2 = "naman";
isPalindromeUsingStringBuilder(input1); //false
isPalindromeUsingStringBuilder(input2); //true
isPalindromeUsingStringUtils(input1); //false
isPalindromeUsingStringUtils(input2); //true
isPalindromeUsingForLoop(input1); //false
isPalindromeUsingForLoop(input2); //true
isPalindromeUsingRecursion(input1); //false
isPalindromeUsingRecursion(input1); //true
Happy Learning !!
Hi Lokesh,
Can you please explain a program to find the longest substring palindrome.
class A
{
int v;
A()
{
calc();
}
public void calc()
{
System.out.println(“A class Method “);
}
}
class B extends A
{
B()
{
calc();
}
public void calc()
{
System.out.println(“B class Method “);
}
public static void main(String ar[])
{
A a1 = new B();
}
}
output
B class Method
B class Method
calc() method in A class is why not executed in A class Constuctor or why execute calc() method of B class
sir plz solve my problem
Because you have override the method. Any method called is always first looked upon class which instance is being created. If not found there, ONLY then super class is looked upon. In both constructor calls, calc() method is found in B so JVM does not go beyond that i.e. A is not looked into.
We can easily do this in java using StringBuilder /StringBuffer also.
StringBuilder str = new StringBuilder(“malayalam”);
System.out.println(“reverse = ” + str.reverse());
then compare original string and reversed one
Good example. You are right. We can use above method as well.
I have to say, the following code doesn’t seem written by someone with 8 Years of rich experience in java technology. Very bad code.
public static boolean checkIntegerPalindrome(int number) { boolean isPalindrome = false; if(number == reverse(number)) { isPalindrome = true; } return isPalindrome; }Please suggest the good way..
Andy, why won’t you just code:
public static boolean checkIntegerPalindrome(int number) {
return number == reverse number;
}
??
It should be reverse(number);
public static boolean checkIntegerPalindrome(int number)
{
return number == reverse(number);
}