Java - Character toCodePoint() Method



The Java Character toCodePoint() method converts the specified surrogate pair to its supplementary code point value.

Supplementary values are not represented in the 16-bit Unicode system. Therefore, surrogate pairs are used to represent these values. These surrogate pairs consist of two characters: a high surrogate code point and a low surrogate code point.

The supplementary code points fall in the range U+10000 to U+10FFFF; however, surrogate pairs fall in ranges: U+D800 to U+DBFF for high surrogates and U+DC00 to U+DFFF for low surrogates.

This method does not validate the specified surrogate pair. The caller must validate it using isSurrogatePair if necessary.

Syntax

Following is the syntax for Java Character toCodePoint() method

public static int toCodePoint(char high, char low)

Parameters

  • high − the high-surrogate code unit

  • low − the low-surrogate code unit

Return Value

This method returns the supplementary code point composed from the specified surrogate pair.

Example

The following example shows the usage of Java Character toCodePoint() method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 char primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = '\ud800';
      ch2 = '\udc00';

      // create an int primitive cp
      int cp;

      // assign code point value of surrogate pair ch1, ch2 to cp
      cp = Character.toCodePoint(ch1, ch2);
      String str = "Supplementary code point value is " + cp;

      // print cp value
      System.out.println( str );
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Supplementary code point value is 65536

Example

In this example given below, we will pass the alphabet as argument characters to the method.

import java.lang.*;
public class LetterDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = 'a';
      ch2 = 'z';
      int cp;
      cp = Character.toCodePoint(ch1, ch2);
      System.out.println("Supplementary code point value is " + cp);
   }
}

Output

Let us compile and run the program above, to obtain the output as given below −

Supplementary code point value is -56514438

Example

Let us change the arguments to the method as the numbered characters (which are ordinary BMP characters) and observe the return values.

import java.lang.*;
public class DigitDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = '0';
      ch2 = '9';
      int cp;
      cp = Character.toCodePoint(ch1, ch2);
      System.out.println("Supplementary code point value is " + cp);
   }
}

Output

The output for the program above after compiling and executing it is given as follows −

Supplementary code point value is -56564679

Example

Following example program takes any two symbols as arguments to the method and returns an invalid supplementary code point as the arguments are BMP characters.

import java.lang.*;
public class SymbolDemo {
   public static void main(String[] args) {
      char ch1, ch2;
      ch1 = '@';
      ch2 = '&';
      int cp;
      cp = Character.toCodePoint(ch1, ch2);
      System.out.println("Supplementary code point value is " + cp);
   }
}

Output

The output for the program above after compiling and executing it is given as follows −

Supplementary code point value is -56548314
java_lang_character.htm
Advertisements