Java2all.com  

The ? Operator





Share This Topic -

It is also known as ternary operator or conditional operator.

expression1 ? expression2 : expression3

b = a < 0 ? -a : a;

EX.

 public class TernaryDemo 
{
	public static void main(String[] args) 
	{
		int a, b;
		a = 50;
		b = a < 0 ? -a : a; // get absolute value of a
		System.out.println("The Absolute value of "+ a + " is " + b);
		a = -10;
		b = a < 0 ? -a : a; // get absolute value of a
		System.out.println("The Absolute value of "+ a + " is " + b);
	}
}

Output :
 
The Absolute value of 50 is 50
The Absolute value of -10 is 10