Signup/Sign In
LAST UPDATED: OCTOBER 20, 2020

Java Character highSurrogate(codepoint) Method

The highSurrogate(int codePoint) method is a part of Character class. This method returns the surrogate pair currently indicating the supplementary character in the UTF-16 encoding for the specified surrogate pair. If the specified character is invalid then an unspecified char is returned.

Syntax:

public static char highSurrogate(int codePoint)  

Parameters:

The parameter passed is the Unicode codepoint supplementary character for the test.

Returns:

Returns the surrogate pair currently indicating the supplementary character in the UTF-16 of the Unicode supplementary character.

Example 1:

Here, the surrogate pair of supplementary character is returned by using the highSurrogate() method.

public class StudyTonight 
{
	public static void main(String[] args) 
	{
		int cp1 = 55;
		int cp2 = 5277;

		char r1 = Character.highSurrogate(cp1);
		char r2 = Character.highSurrogate(cp2);

		System.out.println("The surrogate for  " + (char) cp1 + " is :" + r1);
		System.out.println("The surrogate for  " + (char) cp2 + " is :" + r2);
	}
}


The surrogate for 7 is :?
The surrogate for ? is :?

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 unicode codepoint: ");
			Scanner sc = new Scanner(System. in );
			int i = sc.nextInt();
			char ch = Character.highSurrogate(i);
			System.out.println("The surrogate for  " + (char) i + " is :" + ch);
		}
		catch(Exception e) 
		{
			System.out.println("Invalid Input!!");
		}
	}
}


Enter the unicode codepoint: 856754
The surrogate for ? is :?
*******************************************
Enter the unicode codepoint: 121
The surrogate for y is :?

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.