How to check string is Palindrome or Not in java

check string is palindrome or not in java

How to check string is Palindrome or Not in java

Sometimes we have to do some string related operation in our program and String Palindrome is one of it. In which we have to check String is Palindrome or not. So for that first of all we have to know the meaning of Palindrome and then it’s program for how to do it and both the things are explaining here so let’s see.


Palindrome is a phrase, word, number or character sequence if we read them in reverse order and if it is exactly same as it’s original sequence then it’s know as Palindrome.

Ex. nayan, madam.

If we arrange sequence of character of “nayan” or “madam” in reverse order then we will get exactly same as it’s original sequence. So both are palindrome.

Now, see how to check String is palindrome or not in java program. So in which we have to took one string from the user and reverse it and store it in another String Object.And then compare both the string object if both are same then it’s palindrome otherwise it is not palindrome string.


 Also Read: How to check two strings are anagram or not in java

Also Read: Java program to count number of vowels in a sentence


A Program to check string is palindrome or not in java :


public class StringPali {

    public static void main(String[] args) {

        String org = "nayan";
        String rev = "";

        int length = org.length();

        for (int i = length - 1; i >= 0; i--) 

            rev = rev + org.charAt(i);


        if (org.equals(rev)) 
            System.out.println("String is PALINDROME.");
            System.out.println("String is NOT PALINDROME.");

Download program to check string is palindrome or not, click here.


string is palindrome or not


  • As we can see in above program we took string “nayan” and store it in ‘org’ string object and take another string object ‘rev’ to store string in reverse order. Now read string character in reverse order and store it one by one character in ‘rev’ object. At last compare both ‘rev’ and ‘org’ object the help of equals() method. If both are same then equals() will return true otherwise it returns false.And in our case it will return true so condition will become true so it will execute if part and we will get String is PALINDROME on console.


So I hope this blog is useful to clear your string palindrome concept in your mind very well.

Thank You and Stay Connected with Place4Java and follow on Facebook Page.

Be the first to comment

Leave a Reply

Your email address will not be published.