. Advertisement .
..3..
. Advertisement .
..4..
The question mark and colon operator in Java can be confusing at first. But with proper understanding, you can write more elegant code with it. Let’s learn about java question mark operator through this tutorial.
Java Question Mark Operator
What Is The Question Mark And Colon Operator In Java?
It is the informal name of the only ternary operator in this programming language. The “?:” operator is called “ternary” because it accepts three operands – not two like the other operators in Java.
You can think of this conditional operator as a shorthand for the if-the-else statement. It allows Java programmers to simplify the code and write shorter conditional expressions in place of a regular if block.
The syntax of this ternary operator:
condition ? exppression_1 : expression_2
// java question mark operator
Here, condition is a boolean expression, while expression_1 and expression_2 are two expressions that result in the same data type.
How the question mark and colon operator in Java works:
- The condition is evaluated first.
- If it produces the true boolean value, the program then evaluates expression_1 and ignores the expression_2.
- Otherwise, it only evaluates expression_2 when false is returned.
Here is an example using this operator:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// take input from users
Scanner input = new Scanner(System.in);
System.out.println("Enter your age: ");
double age = input.nextDouble();
// ternary operator checks if
// marks is greater than 40
String result = (age >= 18) ? "are" : "are not";
System.out.println("You " + result + " allowed to continue.");
input.close();
}
}
Output:
Enter your age:
21
You are allowed to continue.
The snippet does a simple check on the age number the user has entered, telling them to go ahead if they are 18 years old or above.
When You Can Use The Question Mark And Colon Operator In Java
The ternary operator can be used in place of a simple if-else statement. So, instead of writing:
if (a > b)
int bigger_number = a;
else
int bigger_number = b;
You can use just one line of code:
int bigger_number = a > b ? a : b;
This line will assign a to the bigger_number variable when it is, in fact, bigger than b. If not, bigger_number receives the value of b. Using the ternary operator makes the code cleaner and more readable.
Remember that while the ternary operator may produce the same results as an if-else code block in some cases, it works in a different way.
First and foremost, the ?: operator only helps you produce an expression. You can’t do more than what expressions in Java can normally do. Meanwhile, a full-fledged if-else code group gives you more capabilities.
Nested Ternary Operator
Like if-else statements, you can chain several ternary operators together to create a nested conditional operator.
Example:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the first number: ");
int x = input.nextInt();
System.out.println("Enter the second number: ");
int y = input.nextInt();
System.out.println("Enter the third number: ");
int z = input.nextInt();
int smallest = (x <= y) ? ((x <= z) ? x : z) : ((y <= z) ? y : z);
System.out.println("Smallest Number: " + smallest);
}
}
Output:
Enter the first number:
4
Enter the second number:
5
Enter the third number:
9
Smallest Number: 4
This program does two rounds of comparison to find the smallest value out of the three given numbers.
Conclusion
The java question mark operator has some niche uses. As a shorthand for the if-else statement, it can make your code shorter and easier to write.
Leave a comment