What is numeric promotion?
5.6 Numeric Promotions in JLS:
Numeric promotion is applied to the operands of an arithmetic operator. Numeric promotion contexts allow the use of an identity conversion or a widening primitive conversion, or an unboxing conversion.
Numeric promotions are used to convert the operands of a numeric operator to a common type so that an operation can be performed. The two kinds of numeric promotion are unary numeric promotion and binary numeric promotion.
Example of unary numeric promotion produces compile error:
byte b = 15; // assign byte value
byte b1 = +b; // result int but required byte. Compiler error
This is covered by "if the operand is of compile-time type byte, short, or char, unary numeric promotion promotes it to a value of type int" by a widening conversion". You must explicit cast the value to byte type.
Examples of Binary Numeric Promotion produces compile-errors:
// result int can not assign to variable's byte
// (covered by "both operands are converted to type int")
byte = byte + byte;
// result float can not assign to variable's int
// (covered by "if either operand is of type float, the other is converted
// to float")
int = float + int;
// result float can not assign to variable's long
// (covered by "if either operand is of type float, the other is converted
// to float")
long = float + long;
// result double can not assign to variable's float
// (covered by "If either operand is of type double, the other is converted
// to double")
float = double + float;
Most Recent java Faqs
- How to uncompress a file in the gzip format?
- How to make a gzip file in Java?
- How to use Java String.split method to split a string by dot?
- How to validate URL in Java?
- How to schedule a job in Java?
- How to return the content in the correct encoding from a servlet?
- What is the difference between JDK and JRE?
Most Viewed java Faqs
- How to read input from console (keyboard) in Java?
- How to use HttpURLConnection POST data to web server?
- How to add BASIC Authentication into HttpURLConnection?
- How to Retrieve Multiple Result Sets from a Stored Procedure in JDBC?
- What are class variables in Java?
- What are local variables in Java?
- How to Use Updatable ResultSet in JDBC?