Java.lang.Character.isLowSurrogate() Method



The Java Character isLowSurrogate() method determines if the given char value is a Unicode low-surrogate code unit (also known as trailing-surrogate code unit).

But what is a surrogate code unit? In Unicode, some characters do not have a valid representation. For these characters, Unicode uses a pair of characters, called as a surrogate pair, to represent supplementary character in UTF-16 encoding. The first character, a high-surrogate code unit, falls into one range of Unicode code points; whereas, the tailing character, a low-surrogate code unit, falls into another range.

Low Surrogates fall in the range of 0xDC00 to 0xDCFF.

Syntax

Following is the syntax for Java Character isLowSurrogate() method

public static boolean isLowSurrogate(char ch)

Parameters

  • ch − the character to be tested

Return Value

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

Example

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

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

      /**
       *  check if ch1, ch2 are Unicode low-surrogate code units
       *  and assign results to b1, b2
       */
      b1 = Character.isLowSurrogate(ch1);
      b2 = Character.isLowSurrogate(ch2);
      String str1 = "ch1 is a Unicode low-surrogate is " + b1;
      String str2 = ch2 + " is a Unicode low-surrogate 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 low-surrogate is true
a is a Unicode low-surrogate is false

Example

We can use the typecasting concept to convert the integer code point into its corresponding char value. This typecasted value is passed as an argument to the method.

import java.lang.*;
public class surrogateDemo {
   public static void main(String args[]) {
      int cp = 0xDC45;
      boolean b;
      b = Character.isLowSurrogate((char)cp);
      System.out.println(b);
   }
}

Output

The program above is compiled and run to obtain the output as follows −

true

Example

In the following example, let us try to pass symbol and digit characters as arguments to the method.

import java.lang.*;
public class surrogateDemo {
   public static void main(String args[]) {
      char c1 = '%';
	   char c2 = '6';
      boolean b1, b2;
      b1 = Character.isLowSurrogate(c);
	   b2 = Character.isLowSurrogate(c);
      System.out.println("The symbol is " + b1 + " surrogate");
	   System.out.println(c2 + " is a Low Surrogate: " + b);
   }
}

Output

Let us compile and run the given example program, the output is obtained as follows −

The symbol is false surrogate
6 is a Low Surrogate: false

Example

We can also use conditional statements (if else) to check whether the argument passed to this method is low surrogate value or not.

import java.lang.*;
public class SurrogateDemo {
   public static void main(String []args){
      char c = '\udc89';
      boolean b = Character.isLowSurrogate(c);
      if(b == true)
         System.out.println(c + " is a Low Surrogate Character");
      else
         System.out.println(c + " is not a Low Surrogate Character");
   }
}

Output

The output after compiling and executing the example program is given as follows −

? is a Low Surrogate Character
java_lang_character.htm
Advertisements