wood burning stoves
The moose likes Beginning Java and the fly likes About numeric promotion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "About numeric promotion" Watch "About numeric promotion" New topic

About numeric promotion

Rohit chandra

Joined: Jun 23, 2007
Posts: 15
byte = byte + byte; // found int, require byte.

How come byte is promoted to int, given the fact that data type is byte and there is no unary operator that would convert byte to int. Does arithmetic operator also convert small type to lager one???
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
Binary numeric promotion takes place. That means that a byte, short, or char are going to promoted before the operation. If the other operand is a byte, short, char, or int, the operand will be promoted to an int. If the other operand is a long, the operand will be promoted to a long. Etc.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Copied and pasted from
Java Language Specification,

Binary numeric promotion is performed on the operands of certain operators:

The multiplicative operators *, / and % (�15.17)
The addition and subtraction operators for numeric types + and - (�15.18.2)
The numerical comparison operators <, <=, >, and >= (�15.20.1)
The numerical equality operators == and != (�15.21.1)
The integer bitwise operators &, ^, and | (�15.22.1)
In certain cases, the conditional operator ? : (�15.25)

The plus and minus also require explicit casting when they are unary operators, thus
byte a = 5;
byte b = -a;
would not compile because the explicit cast is missing.


all events occur in real time
Rohit chandra

Joined: Jun 23, 2007
Posts: 15
Thanks a lot.
I agree. Here's the link: http://aspose.com/file-tools
subject: About numeric promotion
It's not a secret anymore!