• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

byte to char

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm looking at literals and which literal I can implicitly assign to another type.

For example, the following works:



Because short (16 bit) fits into a long (64 bit).

But then why doesn't this work:



A byte is 8 bit, and a char 16 (and unsigned). So why doesn't this work?
 
Ranch Hand
Posts: 198
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it only the size that matters while passing/recieving the data ?
When you passing the short, while method is expecting the long type is fine because short is the type of int/long. so by implicite casting the program runs without any error.
but while you are passing the byte while method is expecting character is not correct because byte can't be cast in to a character.
So, this is not only the size that matters but also the type of data that you are passing. Instead of passing the short if you pass the character type to method and define the method like this


It will work fine because the char is of type Integer, so it can be cast to int type.
 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Kumar Jain wrote:Is it only the size that matters while passing/recieving the data ?
When you passing the short, while method is expecting the long type is fine because short is the type of int/long. so by implicite casting the program runs without any error.
but while you are passing the byte while method is expecting character is not correct because byte can't be cast in to a character.
So, this is not only the size that matters but also the type of data that you are passing. Instead of passing the short if you pass the character type to method and define the method like this


It will work fine because the char is of type Integer, so it can be cast to int type.



This just sounds incorrect to me. If a char is of type int ... and a byte can't be passed to a char, then you are saying that a byte isn't of type int. Aren't you?

Why does the following work then?



This whole typing story just sounds wrong. But maby i'm mistaking. I think it has everything to do with size.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To see why you can't convert a byte to a char implicitly, look at this example. What value would you expect the char to have?

 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:To see why you can't convert a byte to a char implicitly, look at this example. What value would you expect the char to have?



So the issue is signed vs unsigned: possible loss of precision.

??
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The range of byte is -128 to 127. A char can contain values between \u0000 and \uFFFF, or 0 to 65535 in decimal (it's a 16-bit unsigned integer).

The ranges of byte and char do not overlap, and there is no "obvious" way how negative byte values should be mapped to char, so there is no automatic conversion.

On the other hand, the range of int contains the range of byte completely; every possible byte value maps to a corresponding int value, therefore a byte can be automatically converted to an int.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic