Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Character toCodePoint(char high, char low) Method

Java toCodePoint() method is a part of Character class. This method converts the specified surrogate pair to its supplementary code point value.

It must be noted that this method does not validate the specified surrogate pair. The caller must validate it using isSurrogatePair() if necessary.

Syntax:

public static int toCodePoint(char high, char low)

Parameters:

The parameter passed are:

  • high - the high-surrogate code unit
  • low - the low-surrogate code unit

Returns:

Returns the supplementary code point composed from the specified surrogate pair.

Example 1:

Here, the supplementary code point of the specified surrogate pairs is returned.

public class StudyTonight
{  
	public static void main(String[] args)
	{  
		char h1 = '\ud880';  
		char l1 = '\ud110';   
		char h2 = 'I';  
		char l2 = 'O';   

		int r1 = Character.toCodePoint(h1, l1);  
		int r2 = Character.toCodePoint(h2, l2);          

		System.out.println("The result for the above character is given as : "+r1);  
		System.out.println("The result for the above character is given as : "+r2);  
	}  
}


The result for the above character is given as : 193808
The result for the above character is given as : -56539057

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 high = sc.next().charAt(0);  
			char low = sc.next().charAt(0);

			int r = Character.toCodePoint(high, low);
			System.out.println("The result for the given pair is : "+r);  
		}
		catch(Exception e)
		{
			System.out.println("Invalid Input!!");
		}
	}  
}


Enter the character: G y
The result for the given pair is : -56541063
***************************************************
Enter the character: 8 9
The result for the given pair is : -56556487

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.