The conditional operator ?: (a ternary operator) : Conditional Operator « Operators « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Operators » Conditional Operator 
2.9.1.The conditional operator ?: (a ternary operator)
public class MainClass {
  public static void main(String[] argv) {
    boolean x = true;
    int a = 0;
    int b = 1;
    int c = 2;

    if (x) {
      a = b;
    else {
      a = c;
    }

    a = x ? b : c;
    System.out.println(a);
  }
}
1
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.