top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is ternary operator in Java? can you Give me an example this operator ?

+2 votes
311 views
What is ternary operator in Java? can you Give me an example this operator ?
posted May 31, 2017 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

Ternary operator lets you assign a value to a variable based on a boolean expression if true then one value if false then another value. In simple language, a ternary operator is a conditional operator, which can be used as an alternative to the Java if/then/else syntax, but it goes beyond that, and can even be used on the right-hand side of Java statements.It is mainly used when the variables are initialized based on the conditions.

Example

minVal = (a < b) ? a : b;
absValue = (a < 0) ? -a : a;
answer May 31, 2017 by Salil Agrawal
...