Java

What Does ? Mean in Java

Java provides a wide range of operators such as assignment operator “=”, increment operator “++”, decrement operator “–” and so on. You might have heard about all these operators but did you ever hear about a ternary “?” operator? Well, it is nothing but an alternate way of an if-else statement and hence also known as a conditional operator. Moreover, the use of the nested ternary operator “?” provides the functionalities of switch statements.

In this write-up, we are going to explore the various aspects of the ternary “?” operator:

So, let’s begin!

What Does “?” Mean in Java

The “?” in java is nothing but an operator which performs the same way as if-else statements perform and hence it is better known as the conditional or ternary operator in java. The “?” in java assist us in writing a simple, concise, and clear code.

Syntax

The ternary or conditional operator consists of two symbols: one is “?” and the other one is “:” and collectively these two symbols form an expression:

(condition/expression) ? first-expression : second-expression

From the snippet above, it is clear that there are a total of three instances i.e. a specific condition, and two expressions.

How “?” Operator Works in Java

As the “?” operator works similarly to if-else statements so firstly it will test the condition.

  • If true then first-expression will execute.
  • If false then the second-expression will execute.

How “?” Operator works in Java

Let’s consider the below-given examples for a profound understanding of how the “?” operator works in java or how it replaces the if-else statement.

Example

The below-snippet will test a condition using the if-else statement to find out if the provided number is even or odd:

public class TernaryOperatorExample {

    public static void main(String[] args) {
        int number = 35;
       
        if (number % 2 == 0) {
            System.out.println("You Entered an Even Number");
        } else {
            System.out.println("You Entered an Odd Number");
        }
    }

}

The given number will be divided by 2, if the remainder is 0 then it will show an even number, else it shows an odd number:

Output authenticates the appropriateness of the if-else statement.

Example

Now, let’s perform the above example with the help of the “?” operator:

public class TernaryOperatorExample {

    public static void main(String[] args) {
        int number = 35;
        String res = (number % 2 == 0) ? "Even Number" : "Odd Number";
        System.out.println(res);
    }
}

Use of ? operator makes our code concise and easy to understand, now consider the below snippet to see what will be the output of the above-given code:

The above-given snippet verifies the working of the “?” operator.

How to Use Nested “?” Operator in Java

Sometimes we face a scenario where we have to go through multiple conditions in such cases we have to use the multiple “?” operators and hence known as nested ternary “?” operators. Consider the below example for a profound understanding of this concept.

Example

In this example, there are three numbers and among them, we have to find a greater number:

public class TernaryOperatorExample {

  public static void main(String[] args) {
 
    int num1 = 51, num2 = 45, num3 = 55;
   
    int res = (num1 >= num2) ? ((num1 >= num3) ? num1 : num3) : ((num2 >= num3) ? num2 : num3);
 
    System.out.println("Greater number is: " + res);
    }
}

In the above code snippet, we apply the nested ternary operator to find the greatest number among the given numbers and store the result in a variable “res”:

The above snippet shows that “55” is greater than “51” and “45” which shows that the nested “?” operator is working effectively.

Conclusion

In java, the “?” sign is known as a ternary/conditional operator and it performs the same way as if-else statements perform. The “?” in java includes three instances i.e. a specific condition, and two expressions If the specified condition is true then first-expression will execute else second-expression will execute. In the case of multiple conditions, the nested ternary operator can be used. This write-up presents a comprehensive guide for what “?” means in java, and how to use the “?” symbol in java.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.