javaintegervalue-of

Integer.valueOf() static function


 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


Solution

  • 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.