Signup/Sign In

Java Character isHighSurrogate() Method

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

It must be noted that these values can be used to represent supplementary characters in the UTF-16 encoding and cannot be used to represent the characters by themselves.

Syntax:

public static boolean isHighSurrogate(char ch)  

Parameters:

The parameter passed is the character to be checked for the high-surrogate code unit.

Returns:

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

Example 1:

Here, the characters are checked whether they are high-surrogate code units or not.

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		char ch1 = 'A';  
		char ch2 = 'u';  
		char ch3 = '\ud800';  
		char ch4 = '4';  
		char ch5 = '*';  

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

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


A is a high-surrogate?: false
u is a high-surrogate?: false
? is a high-surrogate?: true
4 is a high-surrogate? : false
* is a high-surrogate?: 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.isHighSurrogate(ch);
			System.out.println(ch + " is a high-surrogate? : "+b);
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
}  


Enter the character f
f is a high-surrogate? : false
************************************
Enter the character 8
8 is a high-surrogate? : 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.