Monday, January 13, 2025
HomeProgramming"What does ""?"" mean in Java? - ternary operator"

“What does “”?”” mean in Java? – ternary operator”

In Java, the ? symbol is used as part of the ternary operator, which is a shorthand for an if-else statement. The ternary operator has the following syntax:

condition ? expression1 : expression2;

How it works:

condition: The condition is evaluated. If it’s true, expression1 is executed; if it’s false, expression2 is executed.

See also  Java String to float

expression1: The value returned if the condition is true.

expression2: The value returned if the condition is false.

Example:

int age = 18;
String result = (age >= 18) ? “Adult” : “Minor”;
System.out.println(result); // Outputs: “Adult”

In this example, the ternary operator checks if age is greater than or equal to 18 and assigns “Adult” if true, or “Minor” if false.

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