Question mark operator in java

Question mark in java

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.

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:

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.

Output:

22 is even:true

You can write same logic using question mark operator in java with one line.

Output:

22 is even:true

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:

Output:

23 is Odd

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:

Output:

JAVA2BLOG
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.

Output:

The largest one is : 8

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.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *