Javatpoint Logo
Javatpoint Logo

Automatic Type Promotion in Java

Automatic promotion in Java is a feature that automatically converts smaller data types to larger data types when they are used in expressions or method calls. It is used to ensure that the operands in an expression or the arguments to a method have the same data type, so that the operation can be performed successfully.

Before moving forward, first, we need to know about method overloading and type promotions.

How Automatic Type Promotion Works?

Method Overloading

When a class consists of more than one method with the same name but with different signatures and return types, the methods called overloaded methods, and the process is known as method overloading.

Type Promotion

The name type promotion specifies that a small size datatype can be promoted to a large size datatype. For example, an integer data type can be promoted to long, float, double, etc. Automatic type promotion is performed by JVM when a method that accepts a higher size data type argument is called with the smaller data type.

Widening Primitive Conversion

In Java, data types are ranked based on their size and precision. The ranking order, from lowest to highest, is as follows: byte, short, int, long, float, and double. When two operands with different data types are used in an expression, Java automatically promotes the lower-ranking type to the higher-ranking type before performing the operation.

For example, if a method has a double parameter and we are passing an integer parameter, the compiler promotes an integer to a double.

Type Conversion in Expressions

Numeric promotion is one of the most common applications of automatic promotion in Java. It occurs when arithmetic operations are performed on operands of different data types. Java automatically promotes the operands to a common type before performing the operation.

For example, if we add an int and a double, Java promotes an int to a double before performing the addition. It prevents loss of precision and gives accurate calculations. Numeric promotion follows a hierarchy, with byte and short being promoted to int, int to long, float to double, and so on.



Method Overloading with Automatic Type Promotion in Java

While resolving overloaded methods, if the exact match method is not available, we will not immediately get any compile-time error.

First, the compiler promotes the lower data type argument to the higher data type argument and then checks whether the match method is available or not. If the match method is available, it will be considered.

If the match method is not available, Java compiler promotes argument once again to the higher type. The process will be continued until all possible promotions are not checked.

If the match method is not available, we will get compile-time error. The process is called automatic type promotion in method overloading.



In this example, we are going to look at the overloaded methods and how the automatic type of promotion is happening there.


Automatic Type Promoted to Integer-97
Automatic Type Promoted to Integer-2
Automatic Type Promoted to Double-2.0
Object method called

Next TopicContentPane Java

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA