Character.codePointOf() in Java

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

Syntax:

int java.lang.Character.codePointOf(String name)

This method takes one argument of type String as its parameter. This method returns the code point value of the Unicode character specified by the given Unicode character name.

Parameters: One parameter is required for this method.

name: the Unicode character name.

Returns: the code point value of the character specified by its name.

Throws:

1. IllegalArgumentException - if the specified name is not a valid Unicode character name.

2. NullPointerException - if the name is null.

Approach 1: When no exceptions.

Java

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

        String name = "LEFT SQUARE BRACKET";
        System.out.println(Character.codePointOf(name));
    }
}

Output:

91


Approach 2: IllegalArgumentException 

Java

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

        String name = "hello";
        System.out.println(Character.codePointOf(name));
    }
}


Output:

Exception in thread "main" java.lang.IllegalArgumentException: Unrecognized character name :HELLO at java.base/java.lang.Character.codePointOf(Character.java:11347)


Approach 3: NullPointerException

Java

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

        String name = null;
        System.out.println(Character.codePointOf(name));
    }
}


Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.trim()" because "name" is null at java.base/java.lang.Character.codePointOf(Character.java:11335)


No comments:

Post a Comment