Java.lang.Character.getDirectionality() Method



The java.lang.Character.getDirectionality(char ch) returns the Unicode directionality property for the given character. Character directionality is used to calculate the visual ordering of text. The directionality value of undefined char values is DIRECTIONALITY_UNDEFINED.


Following is the declaration for java.lang.Character.getDirectionality() method

public static byte getDirectionality(char ch)


ch − char for which the directionality property is requested

Return Value

This method returns the directionality property of the char value.




The following example shows the usage of lang.Character.getDirectionality() method.

package com.tutorialspoint;

import java.lang.*;

public class CharacterDemo {

   public static void main(String[] args) {

      // create 2 character primitives ch1, ch2
      char ch1, ch2;

      // assign values to ch1, ch2
      ch1 = 'M';
      ch2 = '\u06ff';

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

      // assign directionality of ch1, ch2 to b1, b2
      b1 = Character.getDirectionality(ch1);
      b2 = Character.getDirectionality(ch2);

       *  byte value 0 represents DIRECTIONALITY_LEFT_TO_RIGHT
       *  byte value 2 represents DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC

      String str1 = "Directionality of " + ch1 + " is " + b1;
      String str2 = "Directionality of ch2 is " + b2;

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

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

Directionality of M is 0 
Directionality of ch2 is 2