aspose file tools
The moose likes Beginning Java and the fly likes Why can't I sum two byte variables ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Why can Watch "Why can New topic
Author

Why can't I sum two byte variables ?

Latif Ben
Greenhorn

Joined: Jun 23, 2011
Posts: 5
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 143

Binary operation (+) in java would return integer which is why there is loss of precision. You have to explicitly cast expression back to byte.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5886
    
    6

Also, to keep your code readable, declare only one variable per line:



Also, don't increase your indent level unless you increase your block depth

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 5886
    
    6

Zeeshan Sheikh wrote:Binary operation (+) in java would return integer which is why there is loss of precision. You have to explicitly cast expression back to byte.


Note, however, that the += operator does the cast for you, so



should work.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Why can't I sum two byte variables ?
 
Similar Threads
byte... x
Question (implicit conversion)
Rules Roundup #108
how to compare the values in arrays
Play video file frame by frame