• 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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
Posts: 23811
140
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
Posts: 23811
140
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
Posts: 23811
140
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!