Java - Character toCodePoint() Method



Description

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.

Getting Supplementary Code Point from Surrogate Pair Example

The following example shows the usage of Java Character toCodePoint() method. In this program, we've created two char variables and assign them values as surrogate pairs. Now using toCodePoint() method, we've retrieved the code point and result is printed.

package com.tutorialspoint;

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

Getting Supplementary Code Point from Alphabet Pair Example

The following example shows the usage of Java Character toCodePoint() method. In this program, we've created two char variables and assign them values as alphabet pairs. Now using toCodePoint() method, we've retrieved the code point and result is printed.

package com.tutorialspoint;

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

Getting Supplementary Code Point from Numbered Char Pairs Example

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

package com.tutorialspoint;

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

Getting Supplementary Code Point from Symbol Pair 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.

package com.tutorialspoint;

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