Character.forDigit() in Java

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

Syntax:

char java.lang.Character.forDigit(int digit, int radix)

This method takes two arguments of type int as its parameters. This method determines the character representation for a specific digit in the specified radix. If the value of radix is not a valid radix, or the value of digit is not a valid digit in the specified radix, the null character('\u005Cu0000') is returned.

Note:

1. The radix argument is valid if it is greater than or equal to MIN_RADIX and less than or equal to MAX_RADIX.

2. The digit argument is valid if 0 <= digit < radix.

3. If the digit is less than 10, then '0' + digit is returned. Otherwise, the value 'a' + digit - 10 is returned.

Parameters: Two parameters are required for this method.

digit: the number to convert to a character.

radix: the radix.

Returns: the char representation of the specified digit in the specified radix.

Approach

Java

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

        int digit = 9;
        int radix = 10;
        System.out.println(Character.forDigit(digit, radix));
    }
}

Output:

9

Explanation:

In this digit is less than 10 so '0'+digit is returns (i.e '0'+'9' = 9).

No comments:

Post a Comment