Open In App

Java Numeric Promotion in Conditional Expression

Last Updated : 22 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, conditional expressions play a very important role in decision-making. To simplify writing conditions, the conditional operator was introduced. It allows us to check a condition, and based on its result, we can choose between two possible values. The operator works by calculating one expression, and depending on whether the condition is true or false, it selects one of two other expressions. Now let’s look at an example for better understanding

Illustration:

Object obj ;

if (true) {

obj = new Integer(4) ;

} else {

obj = new Float(2.0);

}


So, with the usage of conditional operator we expect the expression whose syntax is as follows:

Object obj = true ? new Integer(4) : new Float(2.0);

Note: But the result of running the code gives an unexpected result.

Why is the Result Unexpected?

The unexpected result happens because when two different type of numbers are used, Java automiatically converts one type to match the other, here in the above code the one operand is integer and the another one is float. Java decides to convert the Integer into a Float so both values are the same type. As a result, the Integer 4 becomes Float 4.0, which may not be what we expected.

Note: When Java encounter two different type in the conditional expression, it force one type to match the other which can lead to unexpected result.

Example:

Java
// Java Program to Demonstrate 
// Replacing of Conditional Operator
// with If-else and viceversa

// Importing required classes
import java.io.*;

// Main class
class Geeks {

    // Main driver method
    public static void main (String[] args) {

        // Expression 1 (using ?: )
        // Automatic promotion in conditional expression
        Object o1 = true ? new Integer(4) : new Float(2.0);

        // Printing the output using conditional operator
        System.out.println(o1);

        // Expression 2 (Using if-else)
        // No promotion in if else statement
        Object o2;
        if (true)
            o2 = new Integer(4);
        else
            o2 = new Float(2.0);

        // Printing the output using if-else statement
        System.out.println(o2);
    }
}


Output:


Explanation: The above example demonstrates how java handles type promotion in conditional expression. First the conditional operator compares two value of different types, one is Integer and the another one is Float, Java automatically converts the Integers to a float so that both values become the same and in the second case the integer 4 is assigned without any conversion which result the output as 4.

According to Java Language Specification Section 15.25, the conditional operator will implement numeric type promotion if there are two different types as 2nd and 3rd operand.  The rules of conversion are defined at Binary Numeric Promotion. Therefore, according to the rules given, If either operand is of type double, the other is converted to double and hence 4 becomes 4.0. Whereas, the, if/else construct, does not perform numeric promotion and hence behaves as expected.



Next Article
Article Tags :
Practice Tags :

Similar Reads