Ternary Operator or Conditional Operator(?:)

This operator is called ternary operator because it acts on 3 variables.The other name for this operator is conditional operator.

Syntax:

variable=expression 1?expression 2:expression 3;

First of all,expression 1 is evaluated,If it is true then expression 2 value is stored into the variable.If expression 2 is false,then expression 3 value is stored into the variable.

  1. The only ternary operator available in java is conditional operator(?:)
  2. Example:

     int x=(true)?10:20;
    
  3. We can perform nesting of conditional operator also
  4. Example:

    public class Test {
     public static void main(String[] args) {
    	int x=(false)?10:((false)?100:200);
    	System.out.println(x);
     }
    }
    /*
    Output:
    200
    */
    

    Example:

    public class Test {
     public static void main(String[] args) {
    	 final int a=10,b=20;
    	 byte c=(a>b)?10:20;
    	 System.out.println(c);
     }
    }
    /*
    20
    */
    

Member operator

Member operator is also called dot operator.This operator tells about member of a package or a class.It is used in 3 ways:

  1. We know package contains classes.We can use .operator to refer class of a package.
    Syntax:
    package name.class name;
    Example:
    java.lang.String
  2. We know that each class contains variables and methods.To refer variable of a class,we can use this operator
    Syntax:
    class name.variable name
    Example:
    System.out
  3. Class also contains methods, by using dot operator we can refer methods of a class
    Syntax:
    class name.method name
    object name.method name
    Example:Math.sqrt();