Home Java Character Methods getDirectionality() Java Character getDirectionality() Method Definition In Java, Character.getDirectionality(ch) returns the character's bidirectional class : how it behaves when text mixes left-to-right scripts (such as Latin) with right-to-left scripts (such as Hebrew and Arabic). Latin letters are DIRECTIONALITY_LEFT_TO_RIGHT (0), Hebrew letters DIRECTIONALITY_RIGHT_TO_LEFT (1), and Arabic letters DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC (2).
The result is a byte. Digits, spaces, and punctuation are "weak" or "neutral": their direction depends on nearby text. Undefined chars return DIRECTIONALITY_UNDEFINED (-1).
A static method of java.lang.Character (Java 1.4+): call it on the class, as in Character.getDirectionality(...); you do not need a Character object. No import is needed, because java.lang is always available. The int codePoint overload was added in Java 5.
Unicode version. The answer comes from the Unicode Character Database built into your JDK, and each Java release follows a specific Unicode version (Java 8, for example, uses Unicode 6.2). The characters used on this page have had the same properties for many Unicode versions, so the examples print the same result on any current JDK.
Syntax Pass a char or an int code point; compare the byte result with the constants.
Character.getDirectionality('A') == Character.DIRECTIONALITY_LEFT_TO_RIGHT // true (0)
Character.getDirectionality('\u05D0') == Character.DIRECTIONALITY_RIGHT_TO_LEFT // true (1), Hebrew alef
Character.getDirectionality('5') == Character.DIRECTIONALITY_EUROPEAN_NUMBER // true (3)
Character.getDirectionality(' ') == Character.DIRECTIONALITY_WHITESPACE // true (12) Call What it does byte d = Character.getDirectionality(c);Store the class. d == Character.DIRECTIONALITY_RIGHT_TO_LEFTIs this a (non-Arabic) right-to-left letter? Character.getDirectionality(s.codePointAt(i))The class of a character above U+FFFF.
Two overloads: getDirectionality(char ch) since Java 1.4 and getDirectionality(int codePoint) since Java 5. For full bidirectional layout, use java.text.Bidi.
Library declarations // java.lang.Character
public static byte getDirectionality(char ch) // Java 1.4
public static byte getDirectionality(int codePoint) // Java 5
public static final byte DIRECTIONALITY_UNDEFINED = -1;
public static final byte DIRECTIONALITY_LEFT_TO_RIGHT = 0;
public static final byte DIRECTIONALITY_RIGHT_TO_LEFT = 1; Parameters Parameter Description ch (char)The character to examine. codePoint (int)A Unicode code point.
Return Value Return Value Description byteA DIRECTIONALITY_* constant, or DIRECTIONALITY_UNDEFINED (-1).
Compare with Similar Methods Related properties:
Method How it differs isMirrored(ch)Whether the glyph flips in right-to-left text. getType(ch)The general category, a different property. java.text.BidiLays out whole paragraphs of mixed-direction text.
Java Examples 1. Classes of common chars 2. Detect right-to-left text 3. Code points and bytes Example 1 — The Bidirectional Class of Seven Characters Check letters from three scripts, a digit, a plus sign, a space, and '!'.
public class CharacterGetDirectionalityTable {
static String name(byte d) {
switch (d) {
case Character.DIRECTIONALITY_LEFT_TO_RIGHT: return "LEFT_TO_RIGHT";
case Character.DIRECTIONALITY_RIGHT_TO_LEFT: return "RIGHT_TO_LEFT";
case Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC: return "RIGHT_TO_LEFT_ARABIC";
case Character.DIRECTIONALITY_EUROPEAN_NUMBER: return "EUROPEAN_NUMBER";
case Character.DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR: return "EUROPEAN_NUMBER_SEPARATOR";
case Character.DIRECTIONALITY_WHITESPACE: return "WHITESPACE";
case Character.DIRECTIONALITY_OTHER_NEUTRALS: return "OTHER_NEUTRALS";
default: return "other";
}
}
public static void main(String[] args) {
char[] chars = {'A', '\u05D0', '\u0627', '5', '+', ' ', '!'};
for (char c : chars) {
byte d = Character.getDirectionality(c);
System.out.println("0x" + Integer.toHexString(c) + " " + d + " " + name(d));
}
}
} 0x41 0 LEFT_TO_RIGHT
0x5d0 1 RIGHT_TO_LEFT
0x627 2 RIGHT_TO_LEFT_ARABIC
0x35 3 EUROPEAN_NUMBER
0x2b 4 EUROPEAN_NUMBER_SEPARATOR
0x20 12 WHITESPACE
0x21 13 OTHER_NEUTRALS How It Works Strong classes. Latin 'A' is left-to-right, Hebrew alef (U+05D0) right-to-left, and Arabic alef (U+0627) right-to-left Arabic.Weak classes. The digit and the plus sign are "European number" and "European number separator": they follow the direction of nearby text.Neutral classes. Space and '!' take the direction of their surroundings; a byte switch maps each value to a readable name.Example 2 — Does a String Contain Right-to-Left Letters? Scan for chars whose class is right-to-left or right-to-left Arabic. Only booleans are printed.
public class CharacterGetDirectionalityRtl {
static boolean hasRtl(String s) {
for (int i = 0; i < s.length(); i++) {
byte d = Character.getDirectionality(s.charAt(i));
if (d == Character.DIRECTIONALITY_RIGHT_TO_LEFT
|| d == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC) {
return true;
}
}
return false;
}
public static void main(String[] args) {
System.out.println("English: " + hasRtl("hello"));
System.out.println("Hebrew: " + hasRtl("\u05E9\u05DC\u05D5\u05DD"));
System.out.println("Mixed: " + hasRtl("abc \u0645\u0631\u062D\u0628\u0627"));
System.out.println("Digits: " + hasRtl("2024"));
}
} English: false
Hebrew: true
Mixed: true
Digits: false How It Works Two right-to-left classes. Hebrew letters report class 1 and Arabic letters class 2, so the check accepts both.Mixed text. "abc " is left-to-right, but the Arabic word after it makes the result true.Digits are not strong. European digits are a weak class, so "2024" contains no right-to-left letters.Example 3 — Directionality of Characters Above U+FFFF Use the int overload for a math letter, an emoji, and a Phoenician letter, and store the results as bytes.
public class CharacterGetDirectionalityCodePoints {
public static void main(String[] args) {
byte math = Character.getDirectionality(0x1D400);
byte emoji = Character.getDirectionality(0x1F600);
byte phoenician = Character.getDirectionality(0x10900);
System.out.println("math letter LTR: " + (math == Character.DIRECTIONALITY_LEFT_TO_RIGHT));
System.out.println("emoji neutral: " + (emoji == Character.DIRECTIONALITY_OTHER_NEUTRALS));
System.out.println("Phoenician RTL: " + (phoenician == Character.DIRECTIONALITY_RIGHT_TO_LEFT));
System.out.println("byte values: " + math + ", " + emoji + ", " + phoenician);
}
} math letter LTR: true
emoji neutral: true
Phoenician RTL: true
byte values: 0, 13, 1 How It Works Supplementary letters. MATHEMATICAL BOLD CAPITAL A (U+1D400) is left-to-right, while PHOENICIAN LETTER ALF (U+10900) is right-to-left.Emoji are neutral. U+1F600 has the "other neutrals" class (13), so it follows the surrounding text direction.Stored as bytes. The return type is byte, which easily holds every class value.Frequently Asked Questions What does getDirectionality return? A byte constant for the bidirectional class, such as DIRECTIONALITY_LEFT_TO_RIGHT (0) or DIRECTIONALITY_RIGHT_TO_LEFT (1).
How do I detect Hebrew or Arabic text? Look for chars whose class is DIRECTIONALITY_RIGHT_TO_LEFT or DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC.
What does DIRECTIONALITY_UNDEFINED mean? It is -1, for chars with no defined directionality.
Why do digits and spaces not count as left-to-right? They are weak or neutral classes: their direction depends on nearby letters.
Can getDirectionality lay out mixed text for me? No. It classifies one character. Use java.text.Bidi to analyse whole paragraphs.
Next: getName() Get the official Unicode name of a code point, such as LATIN CAPITAL LETTER A (Java 7+).
getName() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion