• 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

Byte class

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
the follwing code is giving compilation error
Byte e = new Byte(7);
here value passed is with in range of byte but still its asking for external casting from int ot byte

can anybody explain this for me please
thanks
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fix it with new Byte((int)7). <-- wrong!
Fix it with new Byte((byte)7).
Check the java.lang.Byte class' constructors in the Java API documentation.
[ February 27, 2003: Message edited by: Barry Gaunt ]
 
aruna ver
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Byte class constructor needs byte value as its parameter
so in the above example i passed 7 which is in range of byte value, it still asks for explicit type cast into byte value taking 7 as integer
hope i made my doubt clear
thanks
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Aruna,
7 is an integer.
If you want your code to run, it should be declared either as
byte b = 7;
Byte b1 = new Byte(b);
(or)
Byte b = new Byte((byte)7);
hope this helps
Sri
 
aruna ver
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've just woken up with the thought in my head that something was not quite correct about my post above. Sorry Aruna, that cast should have been to byte and not to int. Thanks to Sri for correcting me.
-Barry
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Barry,
I thought that yours was a typo and now I know it for sure
Sri
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it was either not thinking what I was typing, or not typing what I was thinking. But it's getting bad when I wake up in the middle of the night and suddenly had that horrible suspicion I had messed up.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic