It's not a secret anymore!
The moose likes Beginning Java and the fly likes Question about the + operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about the + operator " Watch "Question about the + operator " New topic

Question about the + operator

Juliet Meguma

Joined: Oct 07, 2004
Posts: 5

I'm just reading up on the + operator and the book I'm currently reading states that:

"For a + expression with two operands of primitive numeric types, the result is at least int, because of normal promotion".

Does that mean that two bytes added together will result in an int type, even if the sum of the two bytes is within in the byte range?

Thanks very much!
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
Yes. But you can cast the result to a byte. Bear in mind that doing so could lose data. If you add two bytes valued at 0xFF then cast to a byte, the result will be 0xFE. If you left it at an integer, the result would be 0x1FE.

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
I agree. Here's the link:
subject: Question about the + operator
jQuery in Action, 3rd edition