• Post Reply Bookmark Topic Watch Topic
  • New Topic

wrapper class doubt  RSS feed

 
vijay Thiru
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.
 
Henry Wong
author
Sheriff
Posts: 23291
125
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
 
wise owen
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: 23291
125
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: 23291
125
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!