Monday, January 13, 2025
HomeQ&AJava Ternary operator with Example

Java Ternary operator with Example

The Java ternary operator is a shorthand for an if-else statement. It has the syntax:

condition ? expression1 : expression2;

If the condition is true, expression1 is executed; if false, expression2 is executed.

Example:

int age = 18;

String result = (age >= 18) ? “Adult” : “Minor”;

See also  What does dasvidaniya mean?

System.out.println(result); // Output: Adult

Here, if age is greater than or equal to 18, the result will be “Adult”; otherwise, it will be “Minor”. The ternary operator is a compact alternative to simple conditional checks.

 

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x