Java - Character isHighSurrogate() Method



The Java Character isHighSurrogate() method determines if the given char value is a Unicode high-surrogate code unit (also known as leading-surrogate code unit).

But what are surrogates? Surrogates are a pair of characters in Unicode, that are represented by code points from two special ranges in the system. Surrogates contain two values, high surrogates or leading surrogates, and trailing surrogates or low surrogates.

Such values do not represent characters by themselves, but are used in the representation of supplementary characters in the UTF-16 encoding. High surrogates are encoded from D80016 to DBFF16.

Syntax

Following is the syntax for Java Character isHighSurrogate() method

public static boolean isHighSurrogate(char ch)

Parameters

  • ch − the char value to be tested

Return Value

This method returns true if the char value falls in the range of high surrogates, otherwise false.

Example

The following example shows the usage of Java Character isHighSurrogate() 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 = '\u0c7f';
      ch2 = '\ud8b5';

      // create 2 boolean primitives b1, b2
      boolean b1, b2;

      // assign isHighSurrogate results of ch1, ch2 to b1, b2
      b1 = Character.isHighSurrogate(ch1);
      b2 = Character.isHighSurrogate(ch2);
      String str1 = "ch1 is a Unicode high-surrogate code unit is " + b1;
      String str2 = "ch2 is a Unicode high-surrogate code unit is " + b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Output

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

ch1 is a Unicode high-surrogate code unit is false
ch2 is a Unicode high-surrogate code unit is true

Example

Another sample program that makes use of the conditional statements to determine whether the argument passed is a high-surrogate or not, as the return type of this method is boolean, can be seen below −

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      char ch1 = '\ud800';
      if(Character.isHighSurrogate(ch1))
         System.out.println("The character is a Unicode high-surrogate code unit");
      else
         System.out.println("The character is not a Unicode high-surrogate code unit");
   }
}

Output

The following output is obtained and displayed after the method is executed −

The character is a Unicode high-surrogate code unit

Example

In the following example let us look at another example in which we pass an argument value that is not in the high surrogates’ range

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      char ch1 = '\u0db3';
      if(Character.isHighSurrogate(ch1))
         System.out.println("The character is a Unicode high-surrogate code unit");
      else
         System.out.println("The character is not a Unicode high-surrogate code unit");
   }
}

Output

The following output is obtained and displayed after the method is executed −

The character is not a Unicode high-surrogate code unit

Example

We can also check whether the elements present in a character array are high-surrogates or not.

The given example below declares and initializes a character array. Every element in this array is passed as the argument to this method using loop statements.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      char ch[] = new char[] {'\ud833', '\ub754', '\u0611', '\udbff', '\u5372'};
      int count = 0;
      for(int i = 0; i < ch.length; i++){
         if(Character.isHighSurrogate(ch[i])){
            System.out.println("The character is a Unicode high-surrogate code unit");
            count++;
         }
         else
            System.out.println("The character is not a Unicode high-surrogate code unit");
      }
        if(count == 0)
           System.out.println("Unicode high-surrogate code unit does not exist in this array");
   }
}

Output

Compile and run the program given above and the output will be displayed as follows −

The character is a Unicode high-surrogate code unit
The character is not a Unicode high-surrogate code unit
The character is not a Unicode high-surrogate code unit
The character is a Unicode high-surrogate code unit
The character is not a Unicode high-surrogate code unit
java_lang_character.htm
Advertisements