Character.getName() in Java

Character.getName(): This method is available in java.lang.Character class of Java.

Syntax:

String java.lang.Character.getName(int codePoint)

This method takes one argument of type int as its parameter. This method returns the Unicode name of the specified character codePoint, or null if the code point is unassigned.

Parameters: One parameter is required for this method.

codePoint: the character (Unicode code point).

Returns: the Unicode name of the specified character or null if the code point is unassigned.

Throws: IllegalArgumentException - if the specified codePoint is not a valid Unicode code point

Approach 1: When no exceptions.

Java

public class CharactergetName {
    public static void main(String[] args) {

        int codePoint = 1123;
        System.out.println(Character.getName(codePoint));
    }
}

Output:

CYRILLIC SMALL LETTER YAT


Approach 2: IllegalArgumentException

Java

public class CharactergetName {
    public static void main(String[] args) {

        int codePoint = -1;
        System.out.println(Character.getName(codePoint));
    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: Not a valid Unicode code point: 0xFFFFFFFF at java.base/java.lang.Character.getName(Character.java:11291)


No comments:

Post a Comment