Integer b = Integer.valueOf("444",8);
System.out.println(b);
why b=292 I can't understand this static function
and when
b=Integer.valueOf("444",16);
System.out.println(b)
why b=1092 I appreciate your help Thanks in advance
Integer b = Integer.valueOf("444",8);
System.out.println(b);
why b=292 I can't understand this static function
and when
b=Integer.valueOf("444",16);
System.out.println(b)
why b=1092 I appreciate your help Thanks in advance
As usual sigh the docs are there to read them. http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#valueOf%28java.lang.String,%20int%29
Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument.
This means, if you pass 16 as second argument, the number will be interpreted as a hexadecimal number, thus: 4 * 16 ^ 2 + 4 * 16 + 4 = 1092. Same for octal, only with radix 8.
You are providing the radix as octal and hexa so you are getting the output as per the radix provided:
static Integer valueOf(String s, int radix)
As per the java documentation Integer.valueOf:
Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed integer in the radix specified by the second argument, exactly as if the arguments were given to the parseInt(java.lang.String, int) method. The result is an Integer object that represents the integer value specified by the string.
"444" is the string and 16 is called as the radix, one thing to be noted is that decimal is the default base.
Now the radix is the present base of the argument in this case its 16 i.e. hex which needs to be converted to default i.e. decimal so 444(hex) to decimal is 1092.