EngEd Community

Section’s Engineering Education (EngEd) Program fosters a community of university students in Computer Science related fields of study to research and share topics that are relevant to engineers in the modern technology landscape. You can find more information and program guidelines in the GitHub repository. If you're currently enrolled in a Computer Science related field of study and are interested in participating in the program, please complete this form .

Automatic Type Promotion

December 13, 2021

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.

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

  1. Prerequisites
  2. Primitive data types and the acceptable promotions
  3. Rules for Automatic Type promotion
  4. Automatic Type promotion with method overloading
  5. Problems that may arise due to automatic type promotion
  6. 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:

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, and char must be auto type promoted to int.
  • If any variable taking part in an operation is long, the operation result must be long.
  • If any variable taking part in an operation is float, the operation result must float.

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 be double.

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:

problem

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:

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