Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Is Integer.valueOf("01010",8); correct?

Ranch Hand
Posts: 355
Why does the following compiles?

Usually, we use 2 kinds of static valueOf() methods when it comes to wrapper classes:
valueOf(String arg)
valueOf(String arg, int arg_base)
where arg is the String representation of the primitive and arg_base is the int representation of the radix/base of the arg.
Now, coming to my example code written above, the arg is 01010 which is a binary value, so the base/radix should be 2 right? But, I've used 8 for octal and it still compiles?!! WHY?

Keith Lynn
Ranch Hand
Posts: 2409
Those digits are valid in base 8. Also you will only get a compile-time error if you don't use an int for the second parameter.
[ February 20, 2007: Message edited by: Keith Lynn ]

Remko Strating
Ranch Hand
Posts: 893
I've been playing around with your code.

valueOf(String arg, int arg_base)

The first argument is a string which is used by the translation of the arg_base.

The following code will compile but at runtime throw a NumberFormatException
Integer i = Integer.valueOf("91010",8);

Because 9 is invalid by an octal base.

The following code will also not throw a NumberFormatException
Integer i = Integer.valueOf("F1010",10);
Because F is invalid by an decimal base

With a hexadecimal base the value wil run fine

Integer i = Integer.valueOf("F1010",16);

I hope this helps

Ranch Hand
Posts: 355
Originally posted by Keith Lynn:
Those digits are valid in base 8. Also you will only get a compile-time error if you don't use an int for the second parameter.

[ February 20, 2007: Message edited by: Keith Lynn ]

Are those digits valid in base 16 too? Why? (Kindly give a reference where I can go and read. ) Thanks in advance!

Keith Lynn
Ranch Hand
Posts: 2409
Yes, those digits are valid in base 16.

The binary digits are 0 and 1.

Those digits are valid in all higher bases.

Ranch Hand
Posts: 355
Originally posted by wilhelm tell:
I've been playing around with your code.

valueOf(String arg, int arg_base)

The first argument is a string which is used by the translation of the arg_base.

The following code will compile but at runtime throw a NumberFormatException
Integer i = Integer.valueOf("91010",8);

Because 9 is invalid by an octal base.

The following code will also not throw a NumberFormatException
Integer i = Integer.valueOf("F1010",10);
Because F is invalid by an decimal base

With a hexadecimal base the value wil run fine

Integer i = Integer.valueOf("F1010",16);

I hope this helps

The following code will also not throw a NumberFormatException
Nope! It throws an NFE!

Keith Lynn
Ranch Hand
Posts: 2409
I think that he meant that it will throw it.

Barry Gaunt
Ranch Hand
Posts: 7729
As Keith wrote, the digits 0 and 1 are valid in the octal radix. Remember that 01010 to the compiler is an octal literal because of the leading 0, but you are using a String "01010" of digits in radix 8 which is going to be parsed at runtime, not at compile time.

Barry Gaunt
Ranch Hand
Posts: 7729
As a reference, read the API for class Integer.

Ranch Hand
Posts: 355
Hello there!
Something funny here..hehe..

And here is the output:

Could you figure out why?

Ranch Hand
Posts: 355
Ooooooooooooh!
I got it! It treats it as an octal literal.

Barry Gaunt
Ranch Hand
Posts: 7729
Nothing "funny". If you read my post just above you will see that I have already explained it. Also I suggested that you read the Integer API