Conditional Operators in Java are also known as ternary operators. I’m pretty sure that you are well aware of the concept of the if-else statement in Java. Well, conditional operators are simply a condensed form of the if-else statement which also returns a value. To further simplify the concept, let me discuss this topic in detail with you.
This article will focus on the following pointers:
- What is a Conditional Operator in Java?
- Syntax
- Example
- What is Nested Conditional Operator?
- Example for Nested Operator
Let’s begin!
Starting off with the definition of the conditional operator in Java!
What is a Conditional Operator in Java?
As I have mentioned in the beginning of this article that the conditional operator is also known as the ternary operator, the term ternary is used because this operator consists of three operands which are used to evaluate Boolean expressions. The ultimate aim of the operator is to decide which value is to be assigned to the variable.
After understanding the basic definition of this operator, let us move ahead and grasp the syntax used for its implementation.
Syntax:
It comes with a simple syntax as you can see below:
booleanExpression? expression1: expression2
Explanation: The first expression must be a Boolean expression whereas expression1 and expression2 can be any expression that holds some value. Now, if the first operand evaluates to true then the conditional operator will return expression1 as the output, else expression2 will be returned.
As you are well acquainted with the syntax of the java conditional operator, let’s hop on to our next segment and see the implementation process of this operator.
Example
Here is a sample code:
public class Example { public static void main(String[] args) { int A = 10; int B = 20; String result = A> B ? "A is greater" : "B is greater"; System.out.println(result); } }
Output:
B is greater
Explanation:
You can see how the conditional operator is compared with the two expressions and jumped to the final conclusion. I hope the concept of this operator won’t leave you ambiguous now.
Heading towards our next topic I have nested conditional operator.
What is Nested Conditional Operator?
You can use the conditional operator in nested conditions as well. I have stated at the beginning of this article that conditional operator is the condensed form of an if-else statement, let me prove this to you with an example.
Say, for instance, I have to compare three integer values and find out the largest value amongst them, then the if-else statement would look like this:
if( a> b ) { if ( a > c ) { return "a is greatest"; } else { return "c is greatest"; } else { if( b > c ) { return "b is greatest"; } else { return "c is greatest"; } }
Now, instead of writing this lengthy code, let me condense it by using the concept of the nested conditional operator.
public class NestedExample { public static void main(String[] args) { int a = 10; int b = 20; int c = 30; String result = a > b ? a > c ? "a is greatest" : "c is greatest" : b > c ? "b is greatest" : "c is greatest"; System.out.println(result); } } System.out.println(result); } }
Output:
c is greatest
Here you can see how instead of writing bulky codes, you can simply write one-liner codes by using nested operator and obtain the desired result.
With this, we have reached the end of this article. I hope the content explained above added value to your Java knowledge.
If you found this article on “Conditional Operator in Java” relevant, check out the Edureka’s Java Course, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
If you come across any questions, feel free to ask all your questions in the comments section of “Conditional Operator in Java” and our team will be glad to answer.