Automatic Type Promotion
Type promotion is a common occurrence in Java programming, which can be achieved automatically with primitive data types through the use of autotype promotion. It is also referred to as automatic data type promotion. <!--more--> Sometimes automatic type casting is confused with typecasting, which should not be the case as casting is achieved manually by a programmer during system coding. You should also understand that datatypes once promoted cannot be "depromoted".
Overloading is a concept in Java where different methods can have a common name. The methods, however, differ by the parameters that they take in or even their input data types.
Automatic type casting mainly occurs in method overloading; hence, it is incumbent that we understand both concepts clearly before we begin.
Table of contents
- Prerequisites
- Primitive data types and the acceptable promotions
- Rules for Automatic Type promotion
- Automatic Type promotion with method overloading
- Problems that may arise due to automatic type promotion
- Conclusion
Prerequisites
For a reader to understand this article, they should have:
- A basic understanding of Java programing language.
Primitive data types and the acceptable promotions
- byte
- char
- short
- int
- long
- float
- double
Below is a diagrammatic illustration of possible type promotions:
The Java code fragment below shows the variable declaration and instantiation of 2 variables, num1
and num2
, each byte. It then declares a variable mult1
of type int and is expected to store the products of num1
and num2
.
byte num1 = 200;
byte num2 = 50;
int mult1 = num1*num2;
When the multiplication is done, the product cannot be contained in a byte
location; hence the byte is promoted to int automatically using automatic type promotion, which allows the storage of the product in int
.
Rules for automatic type promotion
The following rules for type promotion must be followed when executing expressions in Java to achieve correct results:
- All variables of the types
byte
,short
, andchar
must be auto type promoted toint
. - If any variable taking part in an operation is
long
, the operation result must belong
. - If any variable taking part in an operation is
float
, the operation result mustfloat
.
The example below illustrates this:
public class AutoTypePromotion {
public static void main(String[] args) {
short num1 = 6;
int num2 = 100;
float num3 = 7.67f;
float result2 = (num2-num1) * num3;
System.out.println("The output is: " + result2);
}
}
The output is 720.98f
, a floating-point value, confirming the rule in question.
- If any variable taking part in an operation is
double
, the operation result must bedouble
.
An example is shown below illustrating this rule:
public class AutoTypePromotion {
public static void main(String[] args) {
int num1 = 100;
double num2 = 0.25;
double result1 = num1/num2;
System.out.println("The output: " + result1);
}
}
The output of 25.0 is a double because the result of the expression automatically becomes a double, as stated by the above rule.
Automatic type promotion with method overloading
package example;
class AutoTypepromotion
{
void average(int x, long y)
{
System.out.println((x+y)/2);
}
void average(int x, int y, int z)
{
System.out.println((x+y+z)3);
}
public static void main(String[] args)
{
AutoTypepromotion s = new AutoTypepromotion();
s.average(20, 24);
s.average(16, 2, 9);
}
}
The code above fragment implements a method overloading by having two functions, each called average, with different parameters. Automatic type promotion is done, making the results long
because one of the operands in operation was long
.
Problems that may arise due to automatic type promotion
As valuable as automatic type promotion is in Java, it may sometimes result in compilation confusions like the one shown in the illustratory diagram below:
This error is caused by automatically promoting the type byte
to the entire execution time. Therefore, typecasting needs to be performed explicitly if it has to be a byte.
Below is a full Java program illustrating Automatic type promotion:
package com.dataflair.typeconversion;
public class AutomaticTypePromotion
{
public static void main(String[] args) {
float num4flt;
int num1integer;
short num2shrt;
byte num3byt;
double num5dble;
long num6lng;
// Above are variable declarations-datatypes are also indicated
num3byt = 27;
num2shrt = num3byt;
num1integer = num2shrt;
num6lng = num1integer;
num4flt = num6lng;
num5dble = num4flt;
// Print statements for outputs
System.out.println("Byte value "+ num3byt);
System.out.println("Short value "+num2shrt);
System.out.println("Integer value"+num1integer);
System.out.println("Long value "+num6lng);
System.out.println("Float value "+num4flt);
System.out.println("Long value "+num5dble);
//This illustrates automatic type promotion
}
}
Output:
Conclusion
Despite the benefits of automatic type promotion, some requirements do not support it. For example, it only supports typecasting or widening.
This means narrowing is uncatered for; hence narrowing has to be done manually by the programmer, which raises the need to learn manual types of casting.
Happy coding!
Peer Review Contributions by: Briana Nzivu