| Author |
About numeric promotion
|
Rohit chandra
Greenhorn
Joined: Jun 23, 2007
Posts: 15
|
|
Hi, 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: 2341
|
|
|
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. Yours, Bu.
|
all events occur in real time
|
 |
Rohit chandra
Greenhorn
Joined: Jun 23, 2007
Posts: 15
|
|
|
Thanks a lot.
|
 |
 |
|
|
subject: About numeric promotion
|
|
|