• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrapper Object!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
The Byte object has a constructor to take Strings.Why the following gives error?Can anybody explain pl! Thanks.
Byte b=new Byte("2.5");
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have to think its because
byte is a signed integers, 8 bits -128 to 127.
Byte b = new Byte("120");
would work...
I think.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I beleive that constructor takes the String arguement as a byte only. Internally, it calls parseByte method which checks if the arguement is a byte or not i.e. there must be only digits.
Else it throws a NumberFormatException.
Sandeep
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another take: how are you going to tell the JVM to ignore the loss of precision?
And another: for performance's sake, it's quite possible that the conversion from String to byte is based on a lookup table of possible values (after all, there's few possible values...). "2.5" would not be among those values as it's not representing a possible value for byte.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the API for Byte.parseByte() (which Byte(String) uses):

An exception of type NumberFormatException is thrown if any of the following situations occurs:
* The first argument is null or is a string of length zero.
* The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
* 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.
* The value represented by the string is not a value of type byte.


[ May 12, 2004: Message edited by: Barry Gaunt ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic