Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculation and functions be it logical, arithmetic, relational etc. They are classified based on the functionality they provide. Here are a few types:
- Arithmetic Operators
- Unary Operators
- Assignment Operator
- Relational Operators
- Logical Operators
- Ternary Operator
- Bitwise Operators
- Shift Operators
This article explains all that one needs to know regarding the Ternary Operator.
Ternary Operator
Java ternary operator is the only conditional operator that takes three operands. It’s a one-liner replacement for if-then-else statement and used a lot in Java programming. We can use the ternary operator in place of if-else conditions or even switch conditions using nested ternary operators. Although it follows the same algorithm as of if-else statement, the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
Syntax:
variable = Expression1 ? Expression2: Expression3
If operates similarly to that of the if-else statement as in Exression2 is executed if Expression1 is true else Expression3 is executed.
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Example:
num1 = 10; num2 = 20; res=(num1>num2) ? (num1+num2):(num1-num2) Since num1<num2, the second operation is performed res = num1-num2 = -10
Flowchart of Ternary Operation

Example 1:
// Java program to find largest among two // numbers using ternary operator import java.io.*; class Ternary { public static void main(String[] args) { // variable declaration int n1 = 5, n2 = 10, max; System.out.println("First num: " + n1); System.out.println("Second num: " + n2); // Largest among n1 and n2 max = (n1 > n2) ? n1 : n2; // Print the largest number System.out.println("Maximum is = " + max); } } |
First num: 5 Second num: 10 Maximum is = 10
Example 2:
// Java code to illustrate ternary operator import java.io.*; class Ternary { public static void main(String[] args) { // variable declaration int n1 = 5, n2 = 10, res; System.out.println("First num: " + n1); System.out.println("Second num: " + n2); // Performing ternary operation res = (n1 > n2) ? (n1 + n2) : (n1 - n2); // Print the largest number System.out.println("Result = " + res); } } |
First num: 5 Second num: 10 Result = -5
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java Ternary Operator Puzzle
- Java Program To Find Largest Between Three Numbers Using Ternary Operator
- && operator in Java with Examples
- & Operator in Java with Examples
- Equality (==) operator in Java with Examples
- Java Unary Operator with Examples
- Java Assignment Operator with Examples
- Diamond operator for Anonymous Inner Class with Examples in Java
- new operator vs newInstance() method in Java
- instanceof operator vs isInstance() method in Java
- new operator in Java
- Double colon (::) operator in Java
- Difference between concat() and + operator in Java
- || operator in Java
- Multiples of 3 and 5 without using % operator
- How can we use Comma operator in place of curly braces?
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.BitSet class in Java with Examples | Set 1
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.frequency() in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


