Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

About numeric promotion

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic