Character.digit() in Java with examples
The java.lang.Character.digit() is an inbuilt method in java which returns the numeric value of the character ch in the specified radix. It returns -1 if the radix is not in the range MIN_RADIX <= radix <= MAX_RADIX or if the value of ch is not a valid digit in the specified radix. A character is a valid digit if at least one of the following is true:
- The method isDigit is true of the character and the Unicode decimal digit value of the character (or its single-character decomposition) is less than the specified radix. In this case the decimal digit value is returned.
- The character is one of the uppercase Latin letters ‘A’ through ‘Z’ and its code is less than radix + ‘A’ – 10. In this case, ch – ‘A’ + 10 is returned.
- The character is one of the lowercase Latin letters ‘a’ through ‘z’ and its code is less than radix + ‘a’ – 10. In this case, ch – ‘a’ + 10 is returned.
- The character is one of the fullwidth uppercase Latin letters A (‘\uFF21’) through Z (‘\uFF3A’) and its code is less than radix + ‘\uFF21’ – 10. In this case, ch – ‘\uFF21’ + 10 is returned.
- The character is one of the fullwidth lowercase Latin letters a (‘\uFF41’) through z (‘\uFF5A’) and its code is less than radix + ‘\uFF41’ – 10. In this case, ch – ‘\uFF41’ + 10 is returned.
Syntax:
public static int digit(char ch, int radix)
Parameters:The function accepts two parameters which are described below:
- ch- This is a mandatory parameter which specifies the character to be converted.
- radix- This is a mandatory parameter which specifies radix.
Return value: This method returns the numeric value represented by the character in the specified radix.
Below programs demonstrates the above method:
Program 1:
// Java program to illustrate the // Character.digit() method import java.lang.*; public class gfg { public static void main(String[] args) { // create and assign value // to 2 character objects char c1 = '3', c2 = '6'; // assign the numeric value of c1 to in1 using radix int in1 = Character.digit(c1, 5); System.out.println("Numeric value of " + c1 + " in radix 5 is " + in1); // assign the numeric value of c2 to in2 using radix int in2 = Character.digit(c2, 15); System.out.println("Numeric value of " + c2 + " in radix 15 is " + in2); } } |
Numeric value of 3 in radix 5 is 3 Numeric value of 6 in radix 15 is 6
Program 2:
// Java program to illustrate the // Character.digit() method // when -1 is returned import java.lang.*; public class gfg { public static void main(String[] args) { // create and assign value // to 2 character objects char c1 = 'a', c2 = 'z'; // assign the numeric value of c1 to in1 using radix int in1 = Character.digit(c1, 5); System.out.println("Numeric value of " + c1 + " in radix 5 is " + in1); // assign the numeric value of c2 to in2 using radix int in2 = Character.digit(c2, 15); System.out.println("Numeric value of " + c2 + " in radix 15 is " + in2); } } |
Numeric value of a in radix 5 is -1 Numeric value of z in radix 15 is -1
Reference: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#digit(char,%20int)
Recommended Posts:
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.Phaser class in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.builtcount() method in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

