javacodex.com
Java Examples
Java Examples
memu home questions

Conditional Operator

This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: "If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result."

Source: (ConditionalOperator.java)

public class ConditionalOperator {
   public static void main(String[] args) {
      String str = ( 1 > 4) ? "yes" : "no";
      System.out.println("str: " + str );
   }
}
 

Output:

# java ConditionalOperator 
str: no

Contact: javacodex@yahoo.com