How to Check if a String is a Palindrome in Java

While dealing with data in Java, there can be the possibility of the contained palindrome data. This data makes it convenient for the developer to access these values since the index is identical from start and end. In such cases, checking if a string is a palindrome in Java does wonders in utilizing and sorting the data conveniently.

This blog will guide the approaches to checking for a palindrome string in Java.

What is a Palindrome?

A string is referred to as a “palindrome” if it is the same pronounced and written reversely as by default.


  • Dad
  • Pop
  • level

How to Check/Verify if a String is a Palindrome in Java?

To check if a string is a palindrome in Java, apply the “for” loop in combination with the “charAt()” and the “equals()” methods. The “for” loop is used to iterate along the elements one by one, the “charAt()” method gives the character at the specified index within a string, and the “equals()” method compares the two strings.


charAt(int index)

In the above syntax, “ind” points to the index of the corresponding element that needs to be referred.


In this syntax, “str” refers to the string that needs to be compared.

Example 1: Check if the Specified String is a Palindrome in Java

In this example, the specified string can be checked for “Palindrome”:

String givenString = "Pop";

String place = "";

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

place= place + givenString.charAt(i);


if (givenString.toLowerCase().equals(place.toLowerCase())) {

System.out.println("The string is a Palindrome");


else {

System.out.println("The string is not a Palindrome");


According to the above code, apply the following steps:

  • Initialize the “String” value to be checked for “Palindrome”.
  • In the next step, define another “String” to accumulate the string in reverse order.
  • Now, apply the “for” loop to iterate through the specified string reversely via the “length” property.
  • Note that “1” is subtracted from the string’s length since the index initiates from “0”.
  • After that, store the iterated values in the allocated “String” variable via the “charAt()” method.
  • Lastly, apply the condition such that the given string and the reversed string both are “equal” via the combined “toLowerCase()” and “equals()” methods and display the corresponding message.
  • Note that the former method is applied to display the palindrome string irrespective of the “case sensitivity”.


In the above output, it can be observed that the specified string is a palindrome, irrespective of the fact that the string contains both the upper case and lower case values.

Example 2: Check if the User Input String is a Palindrome in Java

In this particular example, a user input string can be evaluated for “Palindrome”:

import java.util.Scanner;

Scanner scanner= new Scanner(;

System.out.println("Enter the string");

String input= scanner.nextLine();

if (checkPalindrome(input)) {

System.out.print("The string is palindrome"); }

else {

System.out.print("The string is not a palindrome"); }

scanner.close(); }

public static Boolean checkPalindrome(String givenString) {

String place = "";

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

place= place + givenString.charAt(i);


if (givenString.toLowerCase().equals(place.toLowerCase())) {

return true; }

else {

return false;


In this code block, apply the following steps:

  • First of all, include the “Scanner” class to get the string from the user that needs to be checked for “Palindrome” and “” reads the input string.
  • After that, invoke the function “checkPalindrome()” and pass the input string as its argument.
  • Since the function returns the “boolean” value. Therefore, upon the function being “true”, the former condition will be executed. Otherwise, the latter condition will come into effect.
  • Now, define the function named “checkPalindrome()” having the stated parameter referring to the passed string.
  • In the function definition, recall the discussed approaches for iterating through the passed string reversely and placing it in a separate string.
  • After that, similarly, verify if the given and the reversely iterated strings are “equal” and return the corresponding boolean value based on that.


In this output, it can be analyzed that the user-defined strings are evaluated accordingly.


To check if a string is a “palindrome” using Java, apply the “for” loop in combination with the “charAt()” and the “equals()” methods. These approaches can be utilized to apply a check upon the specified and user input string values irrespective of case sensitivity, respectively. This blog discussed the approaches to verifying if a string is a palindrome using Java.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.