Java ternary operator || ? :


What is java ternary operator

Java ternary operator used in the conditional statement. it is a very interesting operator because it operator work look like same as the work of if....else... statement

Here mention example which helps to understand the concept of the java ternary operator.

Example

public class TernaryOperator {
	public static void main(String[] args) {
		
		int a=10;
		int b=20;
		
		int min= a<b ? a:b;
		int max= a>b ? a:b;
		
		System.out.println(min);
		// first case the condition is true so return the value of a
		System.out.println(max);
		// second case the condition is false so return the value of b
	}

}
in the above example the first case the condition is true so that is why the final output gets the first value which is a. However, in the second test the condition is totally wrong therefore the condition is false so the final output is the second value which is b 

I hope guys you understand the concept of the java ternary operator if you any query regarding this operator so please mention in the comment section i will try to solve this answer

Post a Comment

Previous Post Next Post