jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is Integer.valueOf( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is Integer.valueOf("01010",8); correct?" Watch "Is Integer.valueOf("01010",8); correct?" New topic
Author

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

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 340

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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Dec 28, 2006
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


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 340

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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 31, 2006
Posts: 340

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

Joined: Feb 07, 2005
Posts: 2367
I think that he meant that it will throw it.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
As a reference, read the API for class Integer.
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 340

Hello there!
Something funny here..hehe..

And here is the output:

Could you figure out why?
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 340

Ooooooooooooh!
I got it! It treats it as an octal literal.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
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
Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 340

Yup, I agree..
Thanks for being so kind Barry!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is Integer.valueOf("01010",8); correct?
 
Similar Threads
parse & valueOf
Doubt in K&B SCJP 5: topic 3 valueOf's
toString()
Programmer Certification Study
Radix for Wrapper