
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Numeric Promotion in Conditional Expression
The conditional operator (? :) leverages the output of one value (which is a bool) to decide which expression has to be evaluated next. Let us see an example −
Example
import java.io.*; public class Demo{ public static void main (String[] args){ Object my_obj = true ? new Integer(91) : new Float(89); System.out.println(my_obj); } }
Output
91.0
A class named Demo contains the main function. Here, an object instance is defined and if it is true, an integer value is displayed otherwise a float value is displayed. Next, they are printed on the console.
When promotional expression is not written inside the conditional statement −
Example
import java.io.*; public class Demo{ public static void main (String[] args){ Object obj_2; if (true) obj_2 = new Integer(91); else obj_2 = new Float(89); System.out.println(obj_2); } }
Output
91
A class named Demo contains the main function. Here, an object instance is defined and if it is true, an integer value is assigned to this object. Otherwise, a float value is assigned to this object and then the object is displayed on the console.
- Related Questions & Answers
- C# Numeric Promotion for Conditional Expression
- C# Numeric Promotion
- How to write a conditional expression in lambda expression in Java?
- method overloading and type promotion in Java
- How does MySQL handle overflow during numeric expression assessment?
- Constructor Property Promotion in PHP 8
- What is the conditional operator ?: in Java?
- How MySQL evaluates when I use a conditional expression within SUM() function?
- Regular Expression "[^...]" sub expression in Java
- Select all email addresses beginning with 5 numeric characters (regular expression) in MySQL
- Using underscore in Numeric Literals in Java
- Regular Expression "(re)" Sub-Expression in Java
- Conditional rendering in React.js
- Conditional statements in JavaScript
- Conditional Rendering in ReactJS
Advertisements