• 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

Why char doesn't take byte

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to assign byte to char..but it is error. What is the reason. I thought as byte is of 8 bits i should be able toassign the same to char which is 16 bits.
regrds,
shaker
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good question Baswa
Short answer: byte is signed, char is unsigned.
More details...
Simon Roberts et al on p 105 says

...you can't convert a byte to a char or a char to a short, even though it seems reasonable to do so.


However, there is a very good reason you can't convert a byte to a char.
A char is unsigned and its values can range from 0 to (2**16) - 1 whereas a byte is signed and can range from -128 to 127. A short is signed also, so you have the same problem.
 
Baswa Shaker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thnx,
But why it is possible to assign char to float or double? They are also signed, though they can accomodate more!
 
Baswa Shaker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it because...
char capacity is 2*16-1 (max)
int capacity is 2*31-1 and float/double hold much more than the capacityof char i.e. 2*16-1 (which is far lower than the capacity of int/float/double).
What is the suitable answer?
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Baswa,
You're right - it's possible to convert a char to an int, a float or a double because these are all much wider than a char. For example, looking at the width of the following types
byte -2^7 to 2^7 -1
short -2^15 to 2^15 -1
char 0 to 2^16-1
int -2^31 to 2^31-1
A char can hold a wider range of values than a byte and a short so Java can't automatically convert a char to one of these types.

But an int can hold a much wider range of values than a char so it's fine to convert a char to an int. Same goes for char to a double or a float.
And Greg's right - it's those negative numbers that prevents Java from converting a byte or a short to a char, even though it seems like you should be able to - again, there's a range of numbers (the negative ones) that the char range doesn't include.
If you really need to convert to a narrower datatype(a double to an int, for example), you have to explicitly cast - make sure that you're not going to lose important information if you do this.
Wow, what a longwinded way of saying, you're right!
Kathy
 
Baswa Shaker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thnx Kathy and Greg!!
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi! Hopefully this helps,

byte-->short--> int --> long --> float -->double
char--->int
As long as the direction is upward it OK. But the reverse direction is "not OK".
Assigning short to int--long--float--double is Ok but you cannot assign int to short or short to byte.
As for byte and char, byte is 8 bits and char is 16 bits. The short is also 16 bits. So, the only place a char can fit in, after these two are left out is an int, which is 32 bits.

Please correct me if I am wrong.
Regards,
Preethi
 
Baswa Shaker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Preeti,
U can read it like this also:
char-> int
char-> long
char-> float
char-> double
regards,
shaker
 
reply
    Bookmark Topic Watch Topic
  • New Topic