Illustrate '?' operator.
Advertisements
Question
Illustrate '?' operator.
Code Writing
Short Answer
Advertisements
Solution
The Conditional Operator (?:) in Java is a ternary operator that evaluates a condition and selects one of two expressions depending on whether the condition is true or false. It is a shorter alternative to an if-else statement.
variable = (condition) ? expression1 : expression2;
Example:
class ConditionalOperator
{
public static void main(String[] args)
{
int a = 10, b = 20;
int max = (a > b) ? a : b;
System.out.println("Greater Number: " + max);
}
}
Output:
Greater Number: 20
shaalaa.com
Is there an error in this question or solution?
