Java Character forDigit() Example


Many methods exist in java.lang.Character class used for different purposes. Some methods are useful for checking the character is a digit or letter or is a uppercase letter or lowercase letter etc. At the same time, some methods can modify the case of the letters from uppercase to lowercase or vice versa.

Now the present method is useful to know the radix number of a digit. Go on reading "Java Character forDigit() Example".

Following is the method signature as defined in java.lang.Character class.

  • public static char forDigit(int digit1, int radix1): Returns the character representation of digit digit1 in specified radix format radix1. Returns null (‘\u0000’ which does not print any value) if digit1 or radix1 are invalid numbers.
Following example on Java Character forDigit() Example illustrates.
public class CharacterFunctionDemo
{
 public static void main(String args[])
 {
  System.out.println("9 representation in radix 10 format: " + Character.forDigit(9, 10));   // Octal radix format
  System.out.println("10 representation in radix 16 format: " + Character.forDigit(10, 16)); // Hexa radix format
 }
}


Java Character forDigit() Example
Output screenshot of CharacterFunctionDemo.java on Java Character forDigit() Example

You can try with other numbers and other radix formats in different combinations.

Leave a Comment

Your email address will not be published.