Conditional Operator (? :)

Syntax for conditional operator available in java is given bellow.
variable  = (condition) ? expression-1 : expression-2; 
Example:
  result  = (s=='+') ? (a + b) : (a - b);
The condition is a Boolean statement. If the condition is found to be true, then expression-1 executes and assigned to the variable and if the condition is false then expression-2 executes and assigned to variable. It is very useful, because it perform the action corresponding to true and false condition in a single statement. 
In the example if the value of character variable s is +, then a + b is calculated and assign the result to the variable result. And if the value of s is other than +, then a - b is calculated and assign the result to the variable result. Consider the following full example for getting more details about it.

import java.io.*;
class ConditionalOperatorDemo
{
public static void main(String args[]) throws IOException
{
char s;
int a,b,result;
result = 0;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter first number :");
a =Integer.parseInt( br.readLine());
System.out.println("Enter second number :");
b =Integer.parseInt( br.readLine());
System.out.println("Enter a choice +/-  :");
s = (char)br.read();
result  = (s=='+') ? (a + b) : (a - b);
System.out.println("Result =  "+result);

}

}

Output:

D:\>javac ConditionalOperatorDemo.java
D:\>java  ConditionalOperatorDemo
Enter first number :
7
Enter second number :
2
Enter a choice +/-  :
+
Result =  9
D:\>java  ConditionalOperatorDemo
Enter first number :
6
Enter second number :
1
Enter a choice +/-  :

_

Result =  5