• Post Reply Bookmark Topic Watch Topic
  • New Topic

byte to char  RSS feed

 
Jim Pouwels
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • 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?
 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!