aspose file tools*
The moose likes Beginning Java and the fly likes byte promotion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "byte promotion" Watch "byte promotion" New topic
Author

byte promotion

Dave Ranson
Greenhorn

Joined: Jun 26, 2010
Posts: 2
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
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
See this post: http://www.coderanch.com/t/268738/java-programmer-SCJP/certification/assignement-final-local-variable#1247826
Dave Ranson
Greenhorn

Joined: Jun 26, 2010
Posts: 2
Thanks for your reply, that answers my question exactly!

Cheers
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: byte promotion