This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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


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: 20078
    
  30

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 - OCEJPAD 6
How To Ask Questions How To Answer Questions
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: byte promotion
 
It's not a secret anymore!