Numeric Promotion in Conditional Expression

Conditional Statements in Java

In this article we will be discussing about Numeric Promotion in Conditional Expression in Java.
A bool value’s output is used by the Conditional operator (?:) to determine which expression needs to be evaluated next.

Numeric Promotion in Conditional Expression

Conditional Expression

No matter how much we try to improve our code, if-else conditions in programming often wind up being used repeatedly. Because of this, the conditional operator was created, which makes it simpler for us to write code because it has a specific syntax that it uses to evaluate the condition that is supplied to it.
The boolean value of one expression is used by the conditional operator?: to determine which of the other two expressions should be evaluated.

Syntax for Conditional Expression (?)

variable_name = Expression1 ? Expression2 : Expression3
Conditional expression in java

Prime Course Trailer

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Program To Use Numeric Promotion in Conditional Expression:

Run
// Class object
Object obj ;

// if block
if (true) {
    obj = new Integer(4) ;
}
// else block
else {
    obj = new Float(2.0);
}

Example with Conditional Operator:

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

Related Banners

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription