Java - Character isSurrogate() Method



Description

The Java Character isSurrogate() method is used to determine whether a char value is a surrogate code unit or not. We say surrogate code unit here, as these values do not represent characters by themselves, but are used in the UTF-16 encoding to represent the supplementary code points.

A char value is said to be a surrogate code unit, only if it is either a low surrogate or a high surrogate. Low surrogate units fall in the range of U+DC00 to U+DFFF, whereas, high surrogate units belong to the range U+D800 to U+DBFF.

Syntax

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

public static boolean isSurrogate(char ch)

Parameters

  • ch − A char value that is to be tested.

Return Value

The method returns true if the char argument is a surrogate code unit, and false otherwise.

Checking a char to be Surrogate code unit Example

The following example shows the usage of Java Character isSurrogate() method. In this example, we've created a char variables and assigned it a unicode value. Then using isSurrogate() method, we're checking the surrogate status and result is printed.

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

package com.tutorialspoint;

public class isSurrogateDemo {
   public static void main(String args[]) {
      char ch;
      boolean b;
      ch = '\ud934';
      b = Character.isSurrogate(ch);
      System.out.println("The given char is a surrogate code unit: " + b);
   }
}

Output

Once the program above is compiled and run, the output is produced as follows −

The given char is a surrogate code unit: true

Checking a char to be Surrogate code unit Example

The following example shows the usage of Java Character isSurrogate() method. In this example, we've created a char variables and assigned it a unicode value. Then using isSurrogate() method, we're checking the surrogate status and result is printed.

As the given char represents an ordinary code point belonging to the Basic Multilingual Plane, the method returns false.

package com.tutorialspoint;

public class isSurrogateDemo {
   public static void main(String args[]) {
      char ch;
      boolean b;
      ch = 'a';
      b = Character.isSurrogate(ch);
      System.out.println("The given char is a surrogate code unit: " + b);
   }
}

Output

The output for the given program is displayed as follows −

The given char is a surrogate code unit: false

Checking a char to be Surrogate code unit Example

Since the method returns Boolean type values, conditional statements can be applied to check whether the given argument is a surrogate code unit or not.

package com.tutorialspoint;

public class isSurrogateDemo {
   public static void main(String args[]) {
      char ch;
      boolean b;
      ch = '\udd45';
      b = Character.isSurrogate(ch);
      if(b)
         System.out.println("The given char is a surrogate code unit");
      else
         System.out.println("The given char is not a surrogate code unit");
   }
}

Output

On compiling and executing the given program, the output is obtained as follows −

The given char is a surrogate code unit
java_lang_character.htm
Advertisements