-10
 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

Menna-Allah Sami
  • 580
  • 4
  • 19
  • 4
    Did you check documentation of `valueOf(String data, int radix)`? – Pshemo Aug 17 '15 at 13:26
  • 1
    Have you read [the documentation](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html)? Do you understand what a radix is? That there is a difference between 444₈ and 444₁₆? – RealSkeptic Aug 17 '15 at 13:28

4 Answers4

3

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.

2

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.

Garry
  • 4,493
  • 3
  • 28
  • 48
1

Because 444 in base 8 = 292 in base 10 and 444 in base 16 = 1092 in base 10.

uoyilmaz
  • 3,035
  • 14
  • 25
0

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

Joe C
  • 15,324
  • 8
  • 38
  • 50