• Post Reply Bookmark Topic Watch Topic
  • New Topic

byte + byte = int?  RSS feed

 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was going through a webpage which has details about basic java primitives and I came across this statement:

Any expression involving an int and/or a primitive lower than int (byte, short) will always result in an int.


Does it mean byte + byte = int like 8 bits + 8 bits = 32 bits(which is not right!) ?

I even tried to a bit operation of ADD but that did not give me 32 bits. Could anyone throw some light in this statement?

Thanks!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it means an int + anything smaller than an int will result in an int, just like it says.

A byte + a byte isn't an int plus anything smaller than an int, it's two bytes.
 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, so am doing this:

Please clarify

Thanks!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite simply, a byte plus a byte is an int in Java. The statement you quoted doesn't tell us anything about this either way - but other statements do confirm it. JLS 5.6.2 makes it clear.
When an operator applies binary numeric promotion to a pair of operands, each of which must denote a value that is convertible to a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary:
If any of the operands is of a reference type, unboxing conversion (§5.1.8) is performed. Then:
If either operand is of type double, the other is converted to double.
Otherwise, if either operand is of type float, the other is converted to float.
Otherwise, if either operand is of type long, the other is converted to long.
Otherwise, both operands are converted to type int.


sai prashanth wrote:Does it mean byte + byte = int like 8 bits + 8 bits = 32 bits(which is not right!) ?

It means that Java prefers to treat smaller data types as ints, since any modern processor has at least 32-bit words anyway. A byte + a byte gets converted to an int + and int, and the result is an int. It's easy to add bits here - the new bits are all 0.
 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link. So that makes me curious about something else.

Lets say..'7' - which by itself is an int. Now when I do this:



..am I actually assigning the 32 bits to a 16 bit holder? Does it automatically do a chopping of the higher bits and just store the applicable 16 bits?
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. That's a different rule - '=' is not a binary operator as defined in the JLS, and the section on binary numeric promotion does not apply. Instead it's a special rule for primitive declarations, a shortcut that lets you assign to a byte, short or char at the time of declaration, without casting. And note that if any of the bits being chopped off are not zero, you'd get a compiler error. In this respect it's different from, and I would say better than, a normal cast.
 
sai prashanth
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, agreed. Thanks again for that clarification. One last thing that still makes me wonder is this, infact I have a separate post on this but I will ask the question again here:



Why is this? The method takes a short, and the '9' that I pass in also is under 32 bits then why should I cast it?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sai prashanth wrote:
Why is this? The method takes a short, and the '9' that I pass in also is under 32 bits then why should I cast it?


Simply... Its a different rule. The rule that Mike is referring to (assigning an int literal, that fits in the range of a byte, to a byte) is allowed as part of the support for compile time constants. The compiler is able to figure out at compile time that the value will fit, and hence, the cast is not needed.

This rule does not apply for methods.

Henry
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Never mind about my byte + a byte isn't an int thing, then :)
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getting back to the original question of (byte a) + (byte b), it's about the
+ operator. Before the add, 'a' is promoted to type int, and 'b' is promoted
to type int. So the result is implicitly of type int. It must be assigned to an
int variable or cast back to a byte.

Sai : "short s = 7;" declares a 16 bit signed primitive, not 32.

Jim ... ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!