Ternary operator

Sunday, February 05, 2017 Unknown 0 Comments



In the last post we learned some basic operators. Apart from these operators, there are some other operators we need to know about.

In this post post, we will learn Ternary operator.


Ternary operator(?:) : 
                                   This operator is also called conditional operator. It   contains three operands and is used to evaluate boolean expressions. Goal of this operator is to decide which value should be assigned to variable.
Syntax :
 Variable v=(expression) ? value if true : value if false ;                                                                               
Example : 

  public class TernaryOperatorTest {
  public static void main(String args[]) {
      int x, y;
      x = 5;
      y = (x == 1) ? 10: 15;
      System.out.println( "Value of y : " +  y );
      y = (x == 5) ? 10: 15;
      System.out.println( "Value of y : " + y );
   }
}


Output : 


Value of y : 15
Value of y : 10

Previous topic                                                                  Next topic
Java operators                                                                 instanceof operator







0 comments: