The ? Operator (Ternary) : Ternary Operator « Operators « Java Tutorial






public class MainClass {
  public static void main(String args[]) {
    int i, k;
   
    i = 10;
    k = i < 0 ? -i : i; // get absolute value of i
    System.out.print("Absolute value of ");
    System.out.println(i + " is " + k);
   
    i = -10;
    k = i < 0 ? -i : i; // get absolute value of i
    System.out.print("Absolute value of ");
    System.out.println(i + " is " + k);
  }
}
Absolute value of 10 is 10
Absolute value of -10 is 10








3.8.Ternary Operator
3.8.1.The ? Operator (Ternary)