Last Updated:

Examples of simple java ternary statements

A ternary (triple) java operator is a conditional operator that takes three operands. It is a replacement for the if-then-else expression, which is often used in Java programming. We can use a triple operator to simplify the code, namely to reduce the number of lines, making it concise and visual. How to achieve this will be shown below.

 

Examples of simple ternary operators

One of the most common uses of the ternary operator in Java is to assign the maximum or minimum value of 2 variables to the third variable, replacing the call to the Math.max (a, b) or Math.min (a, b) method.

 

Here is an example that assigns the minimum of 2 variables a and b to the 3rd variable minVal:

 

minVal = (a< b) ? a : b.

In the above code, if the variable a is smaller, the minVal variable is set to a; c. If vice versa, a is greater than b, then minVal will get the value of b.

Attention! In this example, parentheses are not required at all, so you can construct the expressions as follows: minVal = a< b ? a : b.

Parentheses make the code somewhat easier to read, but they're not mandatory, so you can use any sitnaxis you like.

You can use a similar approach to get the absolute value of the number:

intabsValue = (a<0)? -a: a.

In this case, the first operand in the triple operator must be an operator with a logical result or a logical one. If the first operand = rue, the operator returns the second operand, otherwise it returns the third operand.

General syntax

Given the examples listed above, it is obvious that the general syntax looks like this:

result = testCondition ? value1 : value2

Oracledocumentation describes that the application logic is that "If testCondition = true, assign value1 to the result; otherwise, set the result to value2."

Here are two more prime examples. Example 1 – Floating Point Value:

// result is set to 1.0
floatresult = true ?1.0f : 2.0f

Uses of String:

the result is set to "Sorry, man, that's a lie."
Stringresult = false ?» Dude, thatwastrue» :»SorryDude, it’sfalse»;
testCondition is a simple Boolean value or an operator that evaluates a Boolean value (just like the (a<0) operator we discussed above).

Example 2 – Abbot Project Source Code:

privatestaticfinalintsubMenuDelay = Platform.isOSX() ? 100 : 0;

The construct "IF (COND) THEN Statement (s) ELSE Statement (s)" is itself an expression.
«COND ? Statement :Statement" is an expression, and as a result, can be located to the right of the assignment.

Can be used to avoid replicating a function call with a large number of parameters: myFunc ((COND? defaultValue: getMyFuncParameter ())).

Here's also an example where a conditional statement is embedded in a string used to construct a String depending on whether x is a plural or a singular:

returnString = «There » + (x > 1 ? » are » + x + » cookies» :»isonecookie») + » inthejar.»

An example of a similar operation in String. However, for the correct conclusion of the commandment, a specific sex of the person is used here:

returnString = «Thankyou » + (person.isMale() ?» Mr. » :»Ms. «) + person.getLastName() + «.»

Here's another example of Java class source code to test the examples shown in this tutorial:

publicclassJavaTernaryOperatorExamples
{
publicstaticvoidmain(String[]args)
{
// minimum value
intminVal, a=3, b=2;
minVal = a < b ? a : b;
System.out.println("min = " + minVal);
// absolute value
a = -10;
intabsValue = (a < 0) ? -a : a;
System.out.println("abs = " + absValue);
// result is set to 1.0
floatresult = true ?1.0f : 2.0f;
System.out.println("float = " + result);
// result is set to "Sorry dude, that's a lie"
String s = false ? Dude, thatwastrue" :"SorryDude, it's false";
System.out.println(s);
// example of using the ternary operator on the right side of the string
int x = 5;
Stringout = "There" + (x > 1 ? "are" + x + "cookies" :"isonecookie") + "inthejar.";
System.out.println(out);
}
}

Have you been able to deal with the Java ternary operator? Were our examples good? Write your opinion in the comments.