Primitive Numeric Type Promotion Tutorial

When the arithmetic operators (+,-,*,/,%) are used in an expression, Java implements a feature called numeric promotion. You can mix or match any of the numeric primitive data types in an expression. This can cause a problem when it comes time to assign the resulting value of the expression to the target variable. Consider the following:
byte b = 0, a = 6;
int i = 50;
b = a + i ; // this statement will cause an error - why?
Even though we know the result of a + i (56) will fit just fine into a byte data type, the rules of automatic type converions won't let that happen without type casting. In order to solve the problem Java created a new set of rules for type promotion.

  • The expression must be contained inside a pair of opening and closing parenthesis.
  • If any of the operands are type double then the result is promoted to double.
  • Otherwise, if any of the operands are type float, then the result is promoted to float.
  • Otherwise, if any of the operands are type long, then the result is promoted to long.
  • Otherwise, if any or all of the operands are type byte, short, char, or int then the result is promoted to int.
Basically it works like this:
(target variable of data type) (=) (cast of (target data type)) ((operand + operand))
b = (byte) (a + i); // all fixed now

The promotion rules apply to an expression that includes any of the arithmetic operators (+,-,*,/,%). The data types of the original variable declarations are not changed, promotion only applies to the operands and the result of the expression.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md TypePromotion
C:\Java>cd TypePromotion
C:\Java\TypePromotion>Notepad TypePromotion.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class TypePromotion {
	public static void main(String args[]) {
		byte b = 2;
		//b = b * b; // error, the result of  b * b was promoted to an int data type
		//b = (byte) b * b; // still an error, the expression b * b needs to be in parenthesis
		b = (byte) (b * b);
		System.out.println("b = " + b);

		short s = 130;
		char c = 'A';
		s = (short) (s / c);
		System.out.println("s = " + s);

		float f = 2.25F;
		int i = 5;
		f = (float) (f + i);
		System.out.println("f = " + f);

		s = 130;
		long L = 125;
		s = (short) (s % L);
		System.out.println("s = " + s);

		i = 25;
		double d = 12.5D;
		i = (int) (i - d); // remember, casting can result in data loss
		System.out.println("i = " + i);
	}
}

Now switch back to the command prompt (CMD) and type in javac TypePromotion.java and press Enter.
Now type in java TypePromotion and press Enter.


C:\Java\TypePromotion>javac TypePromotion.java
C:\Java\TypePromotion>java TypePromotion
b = 4
s = 2
f = 7.25
s = 5
i = 12


Final thoughts

My tutorials on Primitive Numeric Type Conversions, Primitive Numeric Type Casting, and this tutorial all tie together. It is very helpful to have a solid grasp of how conversion, casting, promotion, and the arithmetic operators all correspond with each other. If this tutorial made complete sense then you are good to go. If not, I would recommend reviewing all three tutorials to obtain a better understanding.


Tutorials