Javatpoint Logo
Javatpoint Logo

Conditional Operator in Java

In Java, conditional operators check the condition and decides the desired result on the basis of both conditions. In this section, we will discuss the conditional operator in Java.

Types of Conditional Operator

There are three types of the conditional operator in Java:

  • Conditional AND
  • Conditional OR
  • Ternary Operator
Operator Symbol
Conditional or Logical AND &&
Conditional or Logical OR ||
Ternary Operator ?:

Conditional AND

The operator is applied between two Boolean expressions. It is denoted by the two AND operators (&&). It returns true if and only if both expressions are true, else returns false.

Expression1 Expression2 Expression1 && Expression2
True False False
False True False
False False False
True True True

Conditional OR

The operator is applied between two Boolean expressions. It is denoted by the two OR operator (||). It returns true if any of the expression is true, else returns false.

Expression1 Expression2 Expression1 || Expression2
True True True
True False True
False True True
False False False

Let's create a Java program and use the conditional operator.

ConditionalOperatorExample.java

Output

true
false 

Ternary Operator

The meaning of ternary is composed of three parts. The ternary operator (? :) consists of three operands. It is used to evaluate Boolean expressions. The operator decides which value will be assigned to the variable. It is the only conditional operator that accepts three operands. It can be used instead of the if-else statement. It makes the code much more easy, readable, and shorter.

Note: Every code using an if-else statement cannot be replaced with a ternary operator.

Syntax:

The above statement states that if the condition returns true, expression1 gets executed, else the expression2 gets executed and the final result stored in a variable.

Conditional Operator in Java

Let's understand the ternary operator through the flowchart.

Conditional Operator in Java

TernaryOperatorExample.java

Output

Value of y is: 90
Value of y is: 61

Let's see another example that evaluates the largest of three numbers using the ternary operator.

LargestNumberExample.java

Output

The largest number is: 89

In the above program, we have taken three variables x, y, and z having the values 69, 89, and 79, respectively. The expression (x > y) ? (x > z ? x : z) : (y > z ? y : z) evaluates the largest number among three numbers and store the final result in the variable largestNumber. Let's understand the execution order of the expression.

Conditional Operator in Java

First, it checks the expression (x > y). If it returns true the expression (x > z ? x : z) gets executed, else the expression (y > z ? y : z) gets executed.

When the expression (x > z ? x : z) gets executed, it further checks the condition x > z. If the condition returns true the value of x is returned, else the value of z is returned.

When the expression (y > z ? y : z) gets executed it further checks the condition y > z. If the condition returns true the value of y is returned, else the value of z is returned.

Therefore, we get the largest of three numbers using the ternary operator.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA