Character.getDirectionality() method in Java with examples

The java.lang.Character.getDirectionality() is an inbuilt method in Java which 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. This method cannot handle supplementary characters. In order to support all the Unicode characters including supplementary characters, pass an integer in the parameter of this method.

Syntax:

public static byte getDirectionality(char ch)

Parameters: The function accepts one parameter ch which is mandatory. This parameter specifies the char for which the directionality property is requested. The parameter can be of int or char data type.



Return value: The function returns byte which denotes the directionality property of the char value.

Below programs illustrate the above method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// Character.getDirectionality() method
// when the passed parameter is an integer
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
        // create 2 int primitives ch1 and
        // ch2 and assign values to them
        int ch1=0x2424, ch2=0x2c61;
  
        // assign directionality of ch1, ch2 to bp1, bp2
        byte b1 = Character.getDirectionality(ch1);
        byte b2 = Character.getDirectionality(ch2);
  
        System.out.println("Directionality of first primitive is " + b1);
        System.out.println("Directionality of first primitive is " + b2);
    }
}

chevron_right


Output:

Directionality of first primitive is 13
Directionality of first primitive is 0

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the
// Character.getDirectionality() method
// when the passed parameter is a character
import java.lang.*;
  
public class gfg {
  
    public static void main(String[] args)
    {
        // create 2 int primitives ch1 and
        // ch2 and assign values to them
        char ch1 = 'M', ch2 = '\u06ff';
  
        // assign directionality of ch1, ch2 to bp1, bp2
        byte b1 = Character.getDirectionality(ch1);
        byte b2 = Character.getDirectionality(ch2);
  
        System.out.println("Directionality of first primitive is " + b1);
        System.out.println("Directionality of first primitive is " + b2);
    }
}

chevron_right


Output:

Directionality of first primitive is 0
Directionality of first primitive is 2

Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getDirectionality(char)



My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.