Java Ternary Operator Puzzle

What Is An Operator In Java Environment?

Operators are some special characters or symbols or data indicators those are able to perform some specific operations in Java environment. There are multiple operands (variables), which can take part here in this operation. After a successful compilation we get the desired output. There are many operators present in Java language, which are mainly used to manipulate the actual values of the key variables present in a Java build code.

For the ternary operation there is a condition followed by the question mark (?), then an expression to execute the method if the condition is true, followed by a colon (: ), and finally the expression to execute if the condition is false. You should only use the ternary statements when the resulting statement is too short to execute the process. Otherwise, you should write a normal statement as a "if statement".

The purpose of the ternary operator is to make the code more concise and readable in the real world environment for the practical purpose. Moving a complex if statement into a ternary operator goes against that goal. The ternary operator in Java programing is a conditional expression which can be used to replace "if....else" condition for some certain operations. In this article, we will learn how to implement the Java Ternary Operator Puzzle by using some logics and algorithms for some certain situations.

What Is A Ternary Operator?

  • ernary operator is a Java environment operator which takes three integer datatypes to perform the particular operation.

  • In Java programing, it is widely used method to bypass the "if....else" condition to reduce the complexity of a code.

  • Being another way to represent the "if...else" method, it follows the same algorithm of that but consumes less space on the memory.

  • There are three parts of the ternary operator; as contains a Boolean expression, a True part and a false part. Depending on these inputs it is decided that the given condition is TRUE or FALSE.

  • Based upon the result of the given condition, the ternary operator works (condition, expression 1, expression 2).

  • One operand is T type (byte), a short of char value and other one is a constant; which represents the type T of a conditional expression only.

Ternary Operator Algorithm

  • Step 1 − Start the program.

  • Step 2 − Declare the variable.

  • Step 3 − Enter the value Of an int

  • Step 4 − By ternary (conditional) operator, check the condition.

  • Step 5 − Display the answer.

  • Step 6 − Terminate the process.


condition to be checked? value_find_if_true : value_find_if_false
class Solutionternaryop {
   public static String getNearestNumber(String number1, int dist1, String number2, int dist2) {
      return "?";
Condition check ? expression given no.1 : expression given no.2;

The statement evaluates to find out the condition is true or false. Here is an example to distinguish the difference between an “if….else” condition and a “ternary operator”.

The “if…..else” Condition:

int x = 10, y = 20, ans;
if (x == 10) {
   if (y == 20) {
      ans = 30;
   } else {
      ans = 50;
} else {
   ans = 0;
printf ("%d\n", ans);

The “Ternary Operator”:

int x = 10, y = 20, ans;
ans = (x == 10 ? (y == 2 ? 30 : 50) : 0);
printf ("%d\n", ans);

Different Approaches to follow:

  • Approach 1 − Finding the largest number among two numbers

  • Approach 2 − Java Ternary Operator Puzzle

Approach 1: Finding the Largest Number among Two Numbers

Here, we have implemented various logics to get the largest number by using Ternary Operator. For this process here, the Complexity of Time and Auxiliary Space both are O(1).

Example 1

public class Ternaryextupoint {
	public static void main(String[] args){	
		int a1 = 50, a2 = 100, max;
		System.out.println("First num coming by as: " + a1);
		System.out.println("Second num coming by as: " + a2);	
		max = (a1 > a2) ? a1 : a2;	
		System.out.println("The Maximum Is Here = " + max);


First num coming by as: 50
Second num coming by as: 100
The Maximum Is Here = 100

Example 2

public class Ternarypotupoint {
	public static void main(String[] args){	
		int s1 = 500, s2 = 100, res;
      System.out.println("First num mentioned in the data: " + s1);
		System.out.println("Second num mentioned in the data: " + s2);
		res = (s1 > s2) ? (s1 + s2) : (s1 - s2);
		System.out.println("Result: Here the largest number is - = " + res);


First num mentioned in the data: 500
Second num mentioned in the data: 100
Result: Here the largest number is - = 600

Example 3

public class Ternarybubooleantupoint {
	public static void main(String[] args){
		boolean condition = true;
		String result = (condition) ? "True It Is" : "False It Is";


True It Is

Ternary Operator Puzzle Using Java Environment

By using the Ternary Operator Puzzle, we can find the statement value is true or false.

Example 1

public class ternaryoppuzzle {
	public static void main(String[] args) {
      char y = 'Y';
      int k = 0;
      System.out.print(true ? y : 0);
      System.out.print(false ? k : y);



After executing the program, we can see that the output is Y89 here. If we decode that, the X is the first statement where 89 is second.


From this article we have learned about the Ternary operator method by using Java language. Here we have experienced lot of advantages of the ternary operator by which we have built the puzzle by using this operator. The readability, performance, bypassing ability of the if-else statements using a nested compact feature makes this feature so unique in Java.

Updated on: 10-Apr-2023


Kickstart Your Career

Get certified by completing the course

Get Started