Ternary operator in Java

Ternary operator in Java

Well, this is a more simplified version of an if-else statement, and it is more readable and understandable. We use this in the cases where our code is short and we want to keep it readable. We are not supposed to use this operator in complicated code since it can leave us confused.

Let’s understand this operator with help of an example. But even before that, we need to understand the syntax of the ternary operator. The ternary operator is nothing but a question mark along with a colon. (?:). We use this in the following manner –

variable = condition? expression1:expression2

Well, you might have understood the thing from the above sample instruction for understanding the syntax, but let me explain the things. In the simple, if-else, we write some condition, on the basis of which we decide whether the if block executes or the else block executes. Now the thing is that the condition is to be written as it is here. We now have to keep in mind two things –

  • If the condition evaluates to true, then expression1 is going to execute.
  • If the condition evaluates to false, then expression2 is going to execute.

So, let us now consider doing a program so that we can understand the implementation of the ternary operator. But remember, this is supposed to be used in some simple cases to increase the readability of our code. Using it in a complicated way can result into confusion. So, below is a program to find the greater number between two numbers.

import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args){
Scanner scanner = new Scanner((System.in));
System.out.println(“Please enter a number: “);
int number1 = scanner.nextInt();
int number2 = scanner.nextInt();
int largest = (number1>number2)? number1:number2;
System.out.println(“Largest number: ” + largest);
}
}

So, if we try running the above program, we can find that the output is the largest number between the two user input numbers. You can understand from the code that the condition is checked and on that basis, we execute either expression1 or expression2. In this case, if the number1 is the largest, it will get stored in the variable largest. Else, the number from number2 will get stored into the variable largest.

So, this becomes very easy to read and understand. This can be done with the simple if-else also, which you can try now on your own. So, we have successfully discussed and implemented the ternary operator.

Now it’s time to move on to some programming exercises. The solutions of these programs are available, but I recommend you to try all those questions on your own so that you can test yourself, and backtrack to the older concepts if needed.

Here are some questions, that you can try around, to get familiar with the concept.

  • Given the ages of Gyani, pandit, and Soham, you have to find the youngest of the three(take the ages as user input)
  • Given the ages of Gyani, pandit, and Soham, you have to find the eldest of the three(take the ages as user input)
  • Write a program to check whether the given number is positive or negative.
  • Write a program to check whether the given number is odd or even
  • Write a program to check whether a given number is divisible by 7.