Signup/Sign In
LAST UPDATED: NOVEMBER 24, 2020

Java Character isWhitepsace(char ch) Method

Java isWhiteSpace(char ch) method is a part of Character class. This method is used to check whether the specified character is whitespace in accordance with Java.

This method does not support supplementary characters. A character is a Java whitespace character if and only if it satisfies one of the following criteria:

  • It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
  • It is '\t', U+0009 HORIZONTAL TABULATION.
  • It is '\n', U+000A LINE FEED.
  • It is '\u000B', U+000B VERTICAL TABULATION.
  • It is '\f', U+000C FORM FEED.
  • It is '\r', U+000D CARRIAGE RETURN.
  • It is '\u001C', U+001C FILE SEPARATOR.
  • It is '\u001D', U+001D GROUP SEPARATOR.
  • It is '\u001E', U+001E RECORD SEPARATOR.
  • It is '\u001F', U+001F UNIT SEPARATOR.

Syntax:

public static boolean isWhitespace(char ch)

Parameters:

The parameter passed is the character to be checked whether it is a Java whitespace character.

Returns:

Returns the boolean value true if the specified character is a Java whitespace character else return false.

Example 1:

Here, the characters are checked whether they are Java whitespace characters or not.

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		char ch1 = '\n';  
		char ch2 = 'd';  
		char ch3 = '\t';  
		char ch4 = 'k';   
		char ch5 = 'P';  

		boolean b1 = Character.isWhitespace(ch1);  
		boolean b2 = Character.isWhitespace(ch2);  
		boolean b3 = Character.isWhitespace(ch3);  
		boolean b4 = Character.isWhitespace(ch4);  
		boolean b5 = Character.isWhitespace(ch5);  

		System.out.println(ch1 +" is Java whitespace character??:  "+b1);  
		System.out.println(ch2 +" is Java whitespace character??:  "+b2);  
		System.out.println(ch3 +" is Java whitespace character??:  "+b3);  
		System.out.println(ch4 +" is Java whitespace character?? : "+b4);  
		System.out.println(ch5 +" is Java whitespace character??:  "+b5);  
	}  
} 


is Java whitespace character??: true
d is Java whitespace character??: false
is Java whitespace character??: true
k is Java whitespace character?? : false
P is Java whitespace character??: false

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent output.

import java.util.Scanner; 
public class StudyTonight
{  
	public static void main(String[] args)
	{  
		try
		{
			System.out.print("Enter the character: ");  
			Scanner sc = new Scanner(System.in);         
			char ch = sc.next().charAt(0);  
			boolean b = Character.isWhitespace(ch);
			System.out.println(ch + " is a Java whitespace character??: "+b);
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
}


Enter the character: t
t is a Java whitespace character??: false
************************************************
Enter the character: n
n is a Java whitespace character??: false

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.