• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, those digits are valid in base 16.

The binary digits are 0 and 1.

Those digits are valid in all higher bases.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that he meant that it will throw it.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a reference, read the API for class Integer.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there!
Something funny here..hehe..

And here is the output:

Could you figure out why?
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooooooooooooh!
I got it! It treats it as an octal literal.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I agree..
Thanks for being so kind Barry!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic