Signup/Sign In

Java Character isSurrogate() Method

Java isSurrogate() method is a part of Character class. This method is used to check whether the specified character is a specified Unicode surrogate code unit or not.

It must be noted that a character value is a surrogate code unit if and only if it is either a low-surrogate code unit or a high-surrogate code unit. Such values do not represent characters by themselves but are used in the representation of supplementary characters in the UTF-16 encoding.

Syntax:

public static boolean isSurrogate(char ch)

Parameters:

The parameter passed is the character to be checked whether it is a surrogate code unit or not.

Returns:

Returns the boolean value true if the specified character is a surrogate code unit else return false.

Example 1:

Here, the characters are checked whether they are a surrogate code unit or not.

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		char ch1 = '\udd10';  
		char ch2 = 'c';  
		char ch3 = '8';  
		char ch4  = '\uf000';   
		char ch5  = '%';  

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

		System.out.println(ch1 +" is surrogate code unit??:  "+b1);  
		System.out.println(ch2 +" is surrogate code unit??:  "+b2);  
		System.out.println(ch3 +" is surrogate code unit??:  "+b3);  
		System.out.println(ch4 +" is surrogate code unit?? : "+b4);  
		System.out.println(ch5 +" is surrogate code unit??:  "+b5);  
	}  
} 


? is surrogate code unit??: true
c is surrogate code unit??: false
8 is surrogate code unit??: false
? is surrogate code unit?? : false
% is surrogate code unit??: 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.isSurrogate(ch);
			System.out.println(ch + " is surrogate code unit?: "+b);
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
}


Enter the character: e
e is surrogate code unit?: false
**************************************
Enter the character: $
$ is surrogate code unit?: 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.