25
AprTernary Operator in Java
Java Programming For Beginners Free Course
Ternary Operator in Java: An Overview
Ternary Operator in Java takes three operands, evaluates the condition, and returns the result. In this Java Tutorial, we'll explore the syntax, providing a comprehensive overview along with practical examples of the Ternary Operator. To further enhance your understanding and application of ternary operator's concepts, consider enrolling in the best Java Certification Course, to gain knowledge about the effective utilization of unary operators for improved problem-solving and time management.
What is a Ternary Operator in Java?
In Java, the ternary operator, also known as the conditional operator, is a shorthand way of writing an if-else statement. It is used to make code more concise and readable by evaluating a Boolean expression and returning one of two values based on the result of the evaluation. The ternary operator has the following syntax:
Syntax:
Variable = Condition ? Expression1 : Expression2
- Condition: It denotes the condition specified in an if statement.
- Expression1: If the condition is met, this expression will be saved in the Variable.
- Expression2: If the condition is false, this expression will be saved in the variable.
- It stores the result returned by either expression in a variable.
Read More - Top 50 Java Interview Questions
Flowchart of Ternary Operation
Example of Ternary Operators in Java
Example 1:
public class TernaryOperatorExample {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = (x > y) ? x : y;
System.out.println("The maximum of " + x + " and " + y + " is: " + result);
}
}
public class TernaryOperatorExample {
public static void main(String[] args) {
int x = 10;
int y = 20;
int result = (x > y) ? x : y;
System.out.println("The maximum of " + x + " and " + y + " is: " + result);
}
}
Explanation
Output
The maximum of 10 and 20 is: 20
Example 2:
public class TernaryOperatorExample {
public static void main(String[] args) {
// Given expression: int x = 10; String message = (x % 2 == 0) ? "Even" : "Odd";
int x = 10;
int y = 11;
String message1 = (x % 2 == 0) ? "Even" : "Odd";
String message2 = (y % 2 == 0) ? "Even" : "Odd";
// Displaying the result
System.out.println("Number " + x + " is " + message1);
System.out.println("Number " + y + " is " + message2);
}
}
Explanation
Output
Number 10 is Even
Number 11 is Odd
Read More - Java Developer Salary In India
Use of Ternary Operator in Java
The ternary operator in Java is a concise and powerful tool used for conditional expressions. Its primary purpose is to simplify the syntax of certain if-else statements, making code more compact and often improving readability. Here are some common use cases for the ternary operator in Java:
1. Conditional Assignment
public class TernaryOperatorExample {
public static void main(String[] args) {
int number = 15;
String result = (number > 0) ? "Positive Number" : "Negative Number";
System.out.println(result);
}
}
Explanation
Output
Positive Number
2. Inline Printing
public class TernaryOperatorExample {
public static void main(String[] args) {
int temperature = 25;
System.out.println("The weather is " + ((temperature > 20) ? "warm" : "cold"));
}
}
Explanation
Output
The weather is warm
3. Nested If-else statement
public class TernaryOperatorExample {
public static void main(String[] args) {
int num1 = 5, num2 = 11, num3 = -15;
int lowest = (num1 <= num2) ? ((num1 <= num3) ? num1 : num3) : ((num2 <= num3) ? num2 : num3);
System.out.println("Minimum Number: " + lowest);
}
}
Explanation
Output
Minimum Number: -15
Advantages of Ternary Operator
- Code Conciseness: Ternary operators reduce the amount of code required for simple conditional assignments.
- Improved Readability: In certain cases, the ternary operator can make the code more readable, especially when dealing with short conditional expressions.
- Inline Usage: Ternary operators can be used inline, making code more compact and reducing the need for additional lines.
- Unary Operator in Java
- Arithmetic Operators in Java
- Relational Operators in Java
- Logical Operators in Java
- Ternary Operator in Java