What do you understand by numeric promotion

What do you understand by numeric promotion ?

Answer : Numeric promotion also known as Implicit casting or java automatic conversions where conversion of a smaller numeric type value to a larger numeric type automatically, so integral and floating-point operations could take place. In numerical promotion or implicit casting the byte, char, and short values will be converted to int values and the int values will be converted to long values, if necessary. The long and float values are converted to double values, as well.Please see example below:

package com.javahonk.implicitCasting;

public class NumericPromotionExample {

    public static void main(String[] args) {// initialize byte
	byte a = 100;
	// implicit or widening or automatic conversion to short
	short b = a;
	// implicit or widening or automatic conversion to int
	int c = b;
	// implicit or widening or automatic conversion to long
	long d = c;
	// implicit or widening or automatic conversion to float
	float e = d;
	// implicit or widening or automatic conversion to double
	double f = e;

	System.out.println("All value implicitely converted:");
	System.out.println("byte: " + a);
	System.out.println("shot: " + b);
	System.out.println("int: " + c);
	System.out.println("long: " + d);
	System.out.println("float: " + e);
	System.out.println("double: " + f);

	// char to int implicit conversion
	char g = 'f';
	int i = g;
	// Please note when java promotes from char to int
	// it will print ASCII value of char
	System.out.println("char to int implicit conversion value: " + i);}

}

 

Output:

What do you understand by numeric promotion

 

Leave a Reply

Your email address will not be published. Required fields are marked *