Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

wrapper class doubt  RSS feed

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Integer wraps the octal value
Integer i = new Integer("012");

Integer wraps the hexadecimal value
Integer i = new Integer("0x12");

but this throw NumberFormatException
can anyone explain this.
 
author
Sheriff
Posts: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Integer wraps the hexadecimal value
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dear Henry Wong,
Integer i = Integer.valueOf("0x12", 16);
throws NumberFormatException.
please explain me
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
thanks in advance.
 
Henry Wong
author
Sheriff
Posts: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



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: 23568
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
thanks in advance.



When I said "That constructor for the Integer class only supports base 10 numbers", I was refering to a particular constructor -- the constructor that takes an String.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!