What is the conditional operator ?: in Java?


The conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate Boolean expressions. The goal of the operator is to decide; which value should be assigned to the variable. The operator is written as:

variable x = (expression)? value if true: value if false

Example

public class Test {
   public static void main(String args[]) {
      int a, b;
      a = 10;
      b = (a == 1) ? 20: 30;
      System.out.println("Value of b is: " + b);
      b = (a == 10) ? 20: 30;
      System.out.println(“Value of b is: " + b);
   }
}

Output

Value of b is: 30
Value of b is: 20

Swarali Sree
Swarali Sree

I love thought experiments.

Updated on: 20-Feb-2020

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements