Character.getNumericValue() in Java

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

Approach 1: When the argument of type char.

Syntax:

int java.lang.Character.getNumericValue(char ch)

This method takes one argument of type char as its parameter. This method returns the int value that the specified Unicode character represents.

Parameters: One parameter is required for this method.

ch: the character to be converted.

Returns: The numeric value of the character, as a non-negative int value; -2 if the character has a numeric value but the value can not be represented as a non-negative int value;-1 if the character has no numeric value.

Exceptions: NA (This method does not throw any exceptions).

Java

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

        char ch = 'm';
        System.out.println(Character.getNumericValue(ch));
    }
}

Output:

22


Approach 2: When the argument of type int.

Syntax:

int java.lang.Character.getNumericValue(int codePoint)

This method takes one argument of type int as its parameter. This method returns the int value that the specified character (Unicode code point) represents.

Parameters: One parameter is required for this method.

codePoint: the character (Unicode code point) to be converted.

Returns: the numeric value of the character, as a nonnegative int value; -2 if the character has a numeric value but the value can not be represented as a nonnegative int value;-1 if the character has no numeric value.

Exceptions: NA( This method does not throw any exceptions).

Java

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

        int codePoint = 'P';
        System.out.println(Character.getNumericValue(codePoint));
    }
}

Output:

25

No comments:

Post a Comment