This week's book giveaway is in the iOS forum.We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# wrapper class doubt

Greenhorn
Posts: 19
Integer wraps the octal value
Integer i = new Integer("012");

Integer i = new Integer("0x12");

but this throw NumberFormatException
can anyone explain this.

author
Sheriff
Posts: 23484
138

Originally posted by vijay Thiru:
Integer wraps the octal value
Integer i = new Integer("012");

Integer i = new Integer("0x12");

but this throw NumberFormatException
can anyone explain this.

That constructor for the Integer class only supports base 10 numbers, so while one will actually compile, neither will do what you want. Anyway, you can either do this...

or this...

Henry

vijay Thiru
Greenhorn
Posts: 19
dear Henry Wong,
Integer i = Integer.valueOf("0x12", 16);
throws NumberFormatException.

Ranch Hand
Posts: 2023
An exception of type NumberFormatException is thrown if any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.

you should use Integer.valueOf("12", 16).

vijay Thiru
Greenhorn
Posts: 19
constructor for the Integer class only supports base 10 numbers

but
Integer i = new Integer(0xc);
but 0xc is not a base 10 number.
how this is not throwing any exception.

Henry Wong
author
Sheriff
Posts: 23484
138

Originally posted by vijay Thiru:
dear Henry Wong,
Integer i = Integer.valueOf("0x12", 16);
throws NumberFormatException.

Sorry for the confusion... it should be...

The "0x" is not needed because the radix is defined in the next parameter.

Henry

Henry Wong
author
Sheriff
Posts: 23484
138

Originally posted by vijay Thiru:
constructor for the Integer class only supports base 10 numbers

but
Integer i = new Integer(0xc);
but 0xc is not a base 10 number.
how this is not throwing any exception.