This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about the + operator

 
Juliet Meguma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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!
Juliet
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic