• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About numeric promotion

 
Rohit chandra
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rohit chandra
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic