what does the following forDigit() method return. static char forDigit(int num,int radix) According to what i have read it returns the digit character associated with the value of num. The following is the code: int num=49; char ch2=Character.forDigit(num,10); System.out.println(ch2); According to me, 1 should be the output but nothing is getting printed,though no compile error is generated.could anyone tell what is wrong with this code? thanx in advance.
Hello i found this piece of info in JLS .Hope this helps. " 20.5.24 public static char forDigit(int digit, int radix) Returns a character that represents the given 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 '\u0000' is returned. A radix is valid if and only if its value is not less than Character.MIN_RADIX (2) and not greater than Character.MAX_RADIX (36). "A digit is valid if and only if it is nonnegative and less than the radix." If the digit is less than 10, then the character value '0'+digit is returned; otherwise, 'a'+digit-10 is returned. Thus, the digits produced by forDigit, in increasing order of value, are the ASCII characters: 0123456789abcdefghijklmnopqrstuvwxyz " Since a digit is valid if and only if it is non-negative and less than radix,in your case 49 is greater than 10 and hence is an invalid digit and a null character is returned.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop