Open In App

Character.getDirectionality() method in Java with examples

Last Updated : 06 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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);
    }
}


Output:

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

Program 2:




// 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);
    }
}


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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads