Before we go for Java code, let's understand what is the palindrome? A palindrome string is a string that reads the same backward as forward and can be of odd or even length. A palindrome number is a number which is equal to its reverse, so basically, if we have the number like "121" this number is palindrome because even if we read it from the right-hand side, again it gives us "121", similarly string like "abba" is a palindrome string.

So let's go through one by one, for various methods to check if string or number is palindrome in java.

Check if string is plaindrome using while loop

package com.palindrome.string;
public class PalindromeStringUsingWhile 
{
	public static void main(String[] args) 
	{
		String word = "abba";
		//Convert to char array and pass it to function
		System.out.println(isPalindrome(word.toCharArray()));
				
		String word2 = "testing";
		System.out.println(isPalindrome(word2.toCharArray()));
	}

	 /**
	  * Test's whether input chars form a palindrome or not.
	  *
	  */
	 public static String isPalindrome(char[] word)
	 {
		 int i = 0;
		 int i2 = word.length - 1;
	    
		 while (i2 > i) 
		 {
			 if (word[i] != word[i2]) 
			 {
				 return "No, string is not a palindrome string";
			 }
			 i++;
			 i2--;
		 }
		 return "Yes, string is a palindrome string";
	 }
}

Output of the above code when excuted on online compiler

Yes, string is a palindrome string
No, string is not a palindrome string

palindrome-program-in-java-using-while-min.png

Explanation:

In the above program, we are passing the string to check to a function, as a char array, then checking string from position 0 & last position (n-1), if matches increment starting position & decrement last position( i ++ & i2 --)

Check again new char at new positions, if char matches keep following the above procedure, if all of the char matches string is palindrome otherwise not.

Check if string is palindrome in java by reversing a string

package com.palindrome.string;
import java.util.Scanner;

public class PalindromeCheck {
 
 
 public static void main(String[] args) {
 
  Scanner scanner = new Scanner(System.in);
  System.out.print("Enter string : ");
 
  String str = scanner.nextLine();
  String reverseStr = "";
 
  for(int i = str.length() - 1; i >= 0; i--){
   reverseStr = reverseStr + str.charAt(i);
  }
 
  if(str.equals(reverseStr)){
   System.out.println("String is palindrome");
  } else {
   System.out.println("String is not palindrome");
  }
 }
}

Output

Enter string : abba
String is palindrome

palindrome-program-java-min.png

Explanation:

In the above program, first of all we are asking user for string this time and saving it in program using Scanner class.

Next, we are reversing the string using looping until we reach at the start of string, then matches the reversed string with main string, if it macthes then string is palindrome otherwise not.

Check number if number is palindrome or not in Java

package com.palindrome.number;
import java.util.Scanner;

public class NumberPalindromeCheck {

	public static void main(String[] args) {
		
		// Taking input from user
		Scanner sc= new Scanner(System.in);
		System.out.println("Please give the input number to check palindrome:");
		int inputByUser= sc.nextInt();
		
		
		int temp= inputByUser;
		int revNumber=0;
		// checking if number is negative
		if(inputByUser<0) 
		{ 
			System.out.println("Negative number.Enter positive number.");
		    System.exit(1);
		} 
		// checking if number is single digit only 
		else if(inputByUser>=0 && inputByUser<=9) 
		{ 
			System.out.println(inputByUser+" is palindomre as it is single digit number.");
			System.exit(1);
		} 
		else 
		{ 
			while(temp>0)
			{
			// extracting last digit of number 
			int rem= temp%10;
			// forming number
			revNumber= revNumber*10+rem;
			// removing last digit from number
			temp=temp/10;
			}
			System.out.println("Input By User:"+inputByUser);
			System.out.println("Reverse number:"+revNumber);
			if(inputByUser==revNumber)
			{
				System.out.println(inputByUser+" is a Palindrome Number");
			}
			else
			{
				System.out.println(inputByUser+" is not a Palindrome Number");
			}
			
		}
		
	}
}

Output

Please give the input number to check palindrome:
121
Input By User:121
Reverse number:121
121 is a Palindrome Number

palindrome-number-check-java-min.png

Using recursion (Number check)

package com.palindrome.number;

import java.util.Scanner;

public class PalindromeNumberRecursion {
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    System.out.println("Enter the number");
	    String str = String.valueOf(sc.nextInt());
	    boolean isPalin = isPalindrome(str, 0, str.length() - 1);
	    if (isPalin) {
	        System.out.println("The number is a palindrome");
	    }
	    else {
	        System.out.println("The number is not a palindrome");
	    }
	}

	private static boolean isPalindrome(String num, int i, int j) {
	    if (i >= j) {
	        return true;
	    }
	    return num.charAt(i) == num.charAt(j) && isPalindrome(num, i + 1, j - 1);
	}
}

Output

Enter the number
442244
The number is a palindrome

palindrome-number-in-java-using-recursion-min.png

Using Custom Function (String check)

public class Main
{
	public static void main(String[] args) {
		System.out.println(isPalindrome("madam"));
	}

	public static boolean isPalindrome(String str) {    
    int n = str.length();
    for( int i = 0; i < n/2; i++ )
        if (str.charAt(i) != str.charAt(n-i-1)) return false;
    return true;    
   }
}

output:

true

You may also like:

Servlet in Java with example (Saving form data in database using servlet)

How to compare Strings in Java?

Pyramid Triangle pattern programs in Java with explanation