What is ternary operator in java how you write it

What is ternary operator in java how you write it ?

or
Is the ternary operator written x : y ? z or x ? y : z ?

Answer : Ternary operator use to check conditional expression. It is also called conditional operator or inline if or ternary if.

Syntax of ternary operator is : (a == b) ? c : d . Please see example below:

package com.javahonk.operatortest;

public class OperatorTest {

    public static void main(String[] args) {
	int i = 10;
	//it will check if i equals 10 if true
	// will print first value otherwise second value
	i = (i == 10 ? 10 : 0);
	System.out.println(i);

    }

}

 

Output:

What is ternary operator in java how you write it

 

Leave a Reply

Your email address will not be published. Required fields are marked *