• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byte promotion

 
Dave Ranson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm having difficulty understanding why I can't write code where a byte (8-bits) is promoted to a char (16-bits).

For example the following code causes the compiler to complain of a possible loss of precision:



Please could someone help me understand why this is the case?

Thanks!

Dave
 
Dave Ranson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply, that answers my question exactly!

Cheers
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A byte can have a value between -128 and -1 whereas char cannot. If the byte was negative you loose that information in the char.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic