Java is one of the most popular programming languages out there, and much like all the other programming languages, Java allows the use of conditional statements as well.
Conditional statements make the very basic fundamentals of any programming language. Conditional statements use logical conditions from mathematics, to perform distinct actions in a code for different decisions. The if else statement is an example of a very popularly used conditional statement.
In this tutorial, we will discuss one such conditional statement, that is the Question mark operator or the ternary operator in java.
Table of Contents
Use of the Question mark colon Operator in java
The question mark and the colon operators, when used together, can be known as a ternary operator.
It consists of three operands, and is therefore also known as a short-hand if...else
operator. The only advantage it has that unlike multiple lines of code written in if...else
statement, ternary operator can be written in a single line of code.
The Question mark and colon
operator is made up of three segments. The first segment is a boolean
expression that returns either true
or false
. The second and third segments are the values that are assigned if the expression returns the boolean
value true
or false
respectively. The second and third segments are separated by the use of a :
sign.
The question mark operator has the syntax as follows:
1 2 3 |
var = condition ? value_if_true : value_if_false |
Further reading:
Here is an example of a simple code that uses the ternary operator.
Let’s say you want to write a program to check whether number is even or odd.
If you have to do it using traditional if else statement, you have to write lot of line of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; public class QuestionMarkOperatorMain { public static void main(String[] args){ int a=22; boolean res; res = isNumberEven(a); System.out.println("22 is even:"+res); } private static boolean isNumberEven(int a) { if(a%2==0) { return true; } else { return false; } } } |
Output:
You can write same logic using question mark operator in java with one line.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.java2blog; public class QuestionMarkOperatorMain { public static void main(String[] args){ int a=22; boolean res; res = a%2==0? true : false; System.out.println("22 is even:"+res); } } |
Output:
As you can see, we are able to write logic to check even odd number with just one line.
The ternary operator can return a value of any type. If we want to get a string value returned instead of a boolean
value, we can do it by making very simple changes to the above program in the following way:
1 2 3 4 5 6 7 8 9 10 11 12 |
package org.arpit.java2blog; public class QuestionMarkOperatorMain { public static void main(String[] args){ int a=23; String result = a%2==0? "Even" : "Odd"; System.out.println("23 is "+result); } } |
Output:
In many case, question mark colon operator or ternary operator is used for null checking to avoid NullPointerException.
For example:
If String is not null, then you want to return String in uppercase. If String is null, then you can return null.
Here is simple logic:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package org.arpit.java2blog.entry; public class QuestionMarkOperatorMain { public static void main(String[] args){ String str1 = "java2blog"; String result1 = toUpperCase(str1); System.out.println(result1); String str2 = null; String result2 = toUpperCase(str2); System.out.println(result2); } public static String toUpperCase(String input) { return input!=null?input.toUpperCase():null; } } |
Output:
null
As easy as the ternary operator may be, we should note that every code that uses the if...else
operator cannot possibly be replicated with the ternary operator.
Use of the nested Question Mark colon Operator
A ternary operator can be used inside another ternary operator.This is called nesting of ternary operator. The nesting of ternary operator to any number of levels is possible in a Java program.
Braces ()
are used wherever necessary in the syntax of the ternary operator to improve the readability of the program.
The following program shows the nesting of the ternary operator.
1 2 3 4 5 6 7 8 9 10 11 |
public class test1{ public static void main(String[] args){ int x = 4; int y = 5; int z = 8; int larger = (x >= y) ? ((x >= z) ? x : z) : ((y >= z) ? y : z); System.out.println("The largest one is : " + larger); } } |
Output:
In the above code, the first test condition (x >= y)
checks whether x
is greater than y
. Then if this condition is true
, then the second that is the (x >= z)
condition is executed. If the first condition is false
, then the third condition which is (y >= z)
is executed.
The nesting of ternary operators makes the code very complex and difficult to maintain, so you should be careful while using ternary operator for very complex logic.
That’s all about question mark in java.