Three-way operator in Java
The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be replaced with ternary operators, and they can even be used to build switch statements by nesting ternary operators. Even though it follows the same mechanism as an if-else statement, the conditional operator takes up less room and speeds up the writing process. It is a simplified if-else expression that yields a value. The only operator in Java that takes three operands is the ternary operator?:
Syntax
expression1 ? expression2 : expression3
Any expression that yields a value may be used as the second and third operands, but the first operand must be a Boolean expression. If the first operand evaluates to true, the ternary construct outputs expression1, else expression2.
The ternary or three way operator is similar to
int a=0;
if(Expression1)
{
a = Expression2;
}
else
{
a = Expression3;
}
Example
n1 = 20;
n2 = 30;
r = (n1>n2) ? (n1-n2):(n1+n2)
Since n1<n2,
the second expression is performed
res = n1+n2 = 40
Ternary Operator Example
int n = 6;
String s = "" ;
if ( n > 20 ) {
s = "Number is greater than 20";
}
else {
s = "Number is less than or equal to 20";
}
Based on the conditional evaluation of num, we have here given the variable msg a value.
This code can be made safer and more readable by simply substituting a ternary construct for the if-else statement:
final String s = n > 20 ? "Number is greater than 20" : "Number is less than or equal to 20";
Program for Three way operator
Threeway.java
// Java program to explain about three way or ternary operator
// Importing java packages
import java.io.* ;
import java .util.* ;
class Main {
public static void main ( String [ ] s )
{
Scanner sc=new Scanner (System .in );
// Declaration
System . out . println ( " Enter the first number " ) ;
int a = sc . nextInt ( ) ;
System . out . println ( " Enter the second number" ) ;
int b = sc . nextInt ( ) ;
int min ;
// printing the first number
System . out . println ( " 1st number is : " + a ) ;
// printing the second number
System . out . println ( " 2nd number : " + b ) ;
// Smallest among two numbers a and b is
min = ( a < b ) ? a : b ;
// if a is less than b then a will be executed or else b will be executed and get stored in the variable min
// Printing the smalllest number
System . out . println ( " Minimum is = " + min ) ;
}
}
Output
Enter the first number
10
Enter the second number
20
1st number is : 10
2nd number : 20
Minimum is = 10
Java program for Three way operator
Threeway2.java
// Java code to illustrate ternary operator
// Java Packages importing
import java . util . *;
import java.io.*;
// public class is declared with name Threeway2
class Threeway2 {
// Main section of the program where execution starts
public static void main ( String [ ] s )
{
// creating the object for scanner class
Scanner sc = new Scanner (System .in );
// Declaration
int a,b;
System . out . println ( " Enter the first number " ) ;
// initialisation of the variables
a = sc . nextInt ( ) ;
System . out . println ( " Enter the second number" ) ;
b = sc . nextInt ( ) ;
// printing the first number
System . out . println ( " 1st number is : " + a ) ;
// printing the second number
System . out . println ( " 2nd number : " + b ) ;
// Performing three way operation
int r = ( a > b ) ? ( a + b ) : ( a - b ) ;
// Printing the result
System . out . println ( " Result = " + r ) ;
}
}
Output
Enter the first number
10
Enter the second number
20
1st number is : 10
2nd number : 20
Result = -10