What does ? mean in Java with examples

In this post, I will be sharing what does ? mean in Java programming. We can use the "?" in the following ways in Java:

1. Ternary operator
2. Wildcard in generics

Read Also: What does colon(:) mean in Java with examples

Let's dive deep into the topic:

What does ? mean in Java

1. Ternary operator


According to Oracle docs, ?: is a conditional operator which can be thought of as shorthand for an if-then-else statement. This conditional operator is also known as the ternary operator in Java because it uses three operands.

Syntax:


In the below example, the ternary operator can be read as if someCondition is true, assign the value of value1 to result. Else, assign the value of value2 to the result.
 result = (someCondition) ? value1 to assign if true : value2 to assign if false


Examples:


Let's consider finding the greater number among two numbers:

 public class TernaryOperatorExample {
    public static void main(String args[]) {
        int a = 5;
        int b = 10;
        int result; 
        // Below code using if-else 
        if(a > b)
            result = a;
        else
            result = b;
        // The above if-else block becomes
        result = (a > b) ? a : b;
        System.out.println(result);
    }
}


Output:
10


Why do we need a ternary operator when Java has an if-else statement?

The ternary operator is much like an if statement except instead of executing a block of code if the condition is true, a ternary operator will assign a value to a variable. In simple words, the goal of the ternary operator is to decide which of two values to assign to a variable.

Another example of a ternary operator is given below:

 public class TernaryOperatorExample2 {
    public static void main(String args[]) {
      int x = 100;
      int y = 1000;
      String result = (x > y) ? "x is greater than y" : " x is less than y";    
      System.out.println(result);
    }
}


Output:
x is less than y


2. Wildcard in generics


According to Oracle docs, ? is called Wildcard as part of Java generics. It represents an unknown type. It can be used in a variety of situations: as the type of a field, local variable, or parameter; sometimes as a return type.
Never use the wildcard as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

Example:


If you want to write the method that works on lists of Number and the subtypes of Number, such as Float, Integer, and Double, you would specify List< ? extends Number>.

Note: If you compare List<Number> with List<? extends Number>, then the term List<Number> is more restrictive than List<? extends Number> because the former matches a list of type Number only whereas the latter matches a list of type Number or any of its subclasses.


 import java.util.*;
public class WildCardExample {
    public static void main(String args[])
    {
        // Below code using a list of Integer objects, prints sum = 18.0
        List<Integer> list1 = Arrays.asList(3, 4, 5, 6);
        System.out.println("sum of integer values: "+ sumOfList(list1));
        /* A list of Double objects using the same sumOfList() method
        , prints sum = 19.0 */
        List<Double> list2 = Arrays.asList(3.2, 4.2, 5.3, 6.3);
        System.out.println("sum of double values: " + sumOfList(list2));
    }
    
    public static double sumOfList(List<? extends Number> list)
    {
        double var = 0.0;
        for( Number num : list)
            var = var + num.doubleValue();
        return var;    
    }
}


Output:
sum of integer values: 18.0
sum of double values: 19.0


That's all for today. Please mention in the comments in case you have any questions related to the what does ? mean in Java programming with examples.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry