Java - Character getDirectionality() Method



Description

The Java Character getDirectionality() method will retrieve the Unicode directionality property for the given character.

Character directionality is used to calculate the visual ordering of text. For example, few languages, like English, are written from left to right; whereas other scripts, like Arabic and Urdu, are written from right to left. However, the Arabic script is not necessarily written from right to left always, the numbers and mathematical dates etc. are written from left to right. In such cases, this method is beneficial to determine the directionality of a character.

Note: The directionality value of undefined char values is DIRECTIONALITY_UNDEFINED.

Syntax

Following is the syntax for Java Character getDirectionality() method

public static byte getDirectionality(char ch)
or,
public static byte getDirectionality(int codePoint)

Parameters

  • ch − char for which the directionality property is requested

  • codePoint − code point for which the directionality property is requested

Return Value

This method returns the directionality property of the char value. The return type is byte.

Getting Unicode Directionality property for the Given Character Example

The following example shows the usage of Java Character getDirectionality() method. In this example, we've created two char variables and assigned them char values as char and unicode resp. Now using getDirectionality() method, we are getting the directionality of the char value and result is printed.

package com.tutorialspoint;

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

Output

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

Directionality of M is 0 
Directionality of ch2 is 2

Getting Unicode Directionality Property for the Special Character Example

The following example shows the usage of Java Character getDirectionality() method. In this example, we've created two char variables and assigned them char values as char and unicode resp. Now using getDirectionality() method, we are getting the directionality of the char value and result is printed.

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      char ch1 = '%';
      char ch2 = '&';
      byte b1 = Character.getDirectionality(ch1);
      byte b2 = Character.getDirectionality(ch2);
      System.out.println("Directionality of " + ch1 + " is " + b1);
      System.out.println("Directionality of ch2 is " + b2);
   }
}

Output

The output for the program above after compiling and executing is as follows −

Directionality of % is 5
Directionality of ch2 is 13

Getting Unicode Directionality Property for the Code Point Example

The following example shows the usage of Java Character getDirectionality() method. In this example, we've created two int variables and assigned them int values as hexadecimal values. Now using getDirectionality() method, we are getting the directionality of the code point values and result is printed.

package com.tutorialspoint;

public class CharacterDemo {
   public static void main(String[] args) {
      int cp1, cp2;
      cp1 = 0x2323;
      cp2 = 0x2c60;
      byte b1, b2;
      b1 = Character.getDirectionality(cp1);
      b2 = Character.getDirectionality(cp2);
      System.out.println("Directionality of cp1 is " + b1);
      System.out.println("Directionality of cp2 is " + b2);
   }
}

Output

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

Directionality of cp1 is 13
Directionality of cp2 is 0

Getting Unicode Directionality Property for the Code Point and a Char Example

The following example shows the usage of Java Character getDirectionality() method. In this example, we've created two int variables and assigned them int values as hexadecimal value and a char. Now using getDirectionality() method, we are getting the directionality of the code point values and result is printed.

package com.tutorialspoint;

public class Demo {
   public static void main(String[] args) {
      int cp1;
      char ch1;
      cp1 = 0x2323;
      ch1 = 'y';
      byte b1, b2;
      b1 = Character.getDirectionality(cp1);
      b2 = Character.getDirectionality(ch1);
      System.out.println("Directionality of cp1 is " + b1);
      System.out.println("Directionality of cp2 is " + b2);
   }
}

Output

If we compile and run the program above, the output is displayed as −

Directionality of cp1 is 13
Directionality of cp2 is 0
java_lang_character.htm
Advertisements