Java - Character highSurrogate() Method



Description

The Java Character highSurrogate() method accepts a code point as an argument and returns the high or leading surrogate value of the surrogate pair that represents a supplementary character in UTF-16 encoding. If the code point is not a valid supplementary code point, an unspecified char value will be returned.

This method is a static method, hence, it can be invoked using the class name as its prefix without creating the instance of the class.

Syntax

Following is the syntax of the Java Character highSurrogate() method.

public static char highSurrogate(int codePoint)

Parameters

  • codePoint − a supplementary character

Return Value

This method returns a leading surrogate character that represents the supplementary character argument.

Getting High Surrogate Value for CodePoint Example

The following example shows the usage of Java Character highSurrogate() method. In this example, we've created an int variables and assigned it a valid codepoint. Using highSurrogate() method, the high surrogate value of codepoint is printed.

package com.tutorialspoint;

public class highSurrogateDemo {
   public static void main(String args[]) {
      int cp;
      char hs;
      cp = 0x10001;
      hs = Character.highSurrogate(cp);
      System.out.println("The high surrogate character is: " + hs);
   }
}

Output

The output of the program above is as follows −

The high surrogate character is: ?

Getting High Surrogate Value for non-supplementary codepoint Example

The following example shows the usage of Java Character highSurrogate() method. In this example, we've created an int variables and assigned it a non-supplementary codepoint. Using highSurrogate() method, the high surrogate value of codepoint is printed.

If the code point passed as an argument is not a supplementary code point, the method will return an unspecified char value.

package com.tutorialspoint;

public class highSurrogateDemo {
   public static void main(String args[]) {
      int cp;
      char hs;
      cp = 10;
      hs = Character.highSurrogate(cp);
      System.out.println("The high surrogate character is: " + hs);
   }
}

Output

If we compile and run the above program, the output is displayed as follows −

The high surrogate character is: ?

Getting High Surrogate Value for invalid codepoint Example

The following example shows the usage of Java Character highSurrogate() method. In this example, we've created an int variables and assigned it an invalid codepoint. Using highSurrogate() method, the high surrogate value of codepoint is printed.

package com.tutorialspoint;

public class highSurrogateDemo {
   public static void main(String args[]) {
      int cp;
      char hs;
      cp = 783260121;
      hs = Character.highSurrogate(cp);
      System.out.println("The high surrogate character is: " + hs);
   }
}

Output

When we compile and run the above program, the output is shown as follows −

The high surrogate character is: ?
java_lang_character.htm
Advertisements