The ternary operator ? : has three operands and takes the following form: operand1 ? operand2 : operand3 : Conditional Operator « Operators « SCJP






class MainClass{
 public static void main(String args[]) {
  for(int i=0;i<10;++i)
   System.out.print((i%3==0)? ("" + i) : "x");
 }
}








2.9.Conditional Operator
2.9.1.The conditional operator ?: (a ternary operator)
2.9.2.The ternary operator ? : has three operands and takes the following form: operand1 ? operand2 : operand3
2.9.3.a = x ? b : c; The types b and c should be compatible.
2.9.4.a = x ? b : c; The type of the expression x should be boolean.
2.9.5.nest conditional operators into one statement