Java/J2EE and SOA (239 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (46 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (239 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

What is Conditional Operator in Java and how to write it?

Published on Aug 21,2019 127 Views
7 / 90 Blog from Java Fundamentals

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Conditional Operators in Java are also known as ternary operators. I’m pretty sure that you are well aware of the concept of the if-else statement in Java. Well, conditional operators are simply a condensed form of the if-else statement which also returns a value. To further simplify the concept, let me discuss this topic in detail with you.

This article will focus on the following pointers:

Let’s begin!
Starting off with the definition of the conditional operator in Java!

What is a Conditional Operator in Java?

As I have mentioned in the beginning of this article that the conditional operator is also known as the ternary operator, the term ternary is used because this operator consists of three operands which are used to evaluate Boolean expressions. The ultimate aim of the operator is to decide which value is to be assigned to the variable.

Conditional Operator for in Java - Edureka

After understanding the basic definition of this operator, let us move ahead and grasp the syntax used for its implementation.

Syntax:

It comes with a simple syntax as you can see below:

booleanExpression? expression1: expression2

Explanation: The first expression must be a Boolean expression whereas expression1 and expression2 can be any expression that holds some value. Now, if the first operand evaluates to true then the conditional operator will return expression1 as the output, else expression2 will be returned.

As you are well acquainted with the syntax of the java conditional operator, let’s hop on to our next segment and see the implementation process of this operator.

Moving on with an example.

Example

Here is a sample code:

 public class Example
{
public static void main(String[] args)
{
int A = 10;
int B = 20;
String result = A> B ? "A is greater" : "B is greater";
System.out.println(result);
}
} 

Output:
B is greater

Explanation:

You can see how the conditional operator is compared with the two expressions and jumped to the final conclusion. I hope the concept of this operator won’t leave you ambiguous now.

Heading towards our next topic I have nested conditional operator.

What is Nested Conditional Operator?

You can use the conditional operator in nested conditions as well. I have stated at the beginning of this article that conditional operator is the condensed form of an if-else statement, let me prove this to you with an example.

Example

Say, for instance, I have to compare three integer values and find out the largest value amongst them, then the if-else statement would look like this:

 if( a> b )
{
if ( a > c )
{
return "a is greatest";
}
else
{
return "c is greatest";
}
else
{
if( b > c )
{
return "b is greatest";
}
else
{
return "c is greatest";
}
}

Now, instead of writing this lengthy code, let me condense it by using the concept of the nested conditional operator.

 public class NestedExample
{
public static void main(String[] args)
{
int a = 10;
int b = 20;
int c = 30;
String result = a > b ? a > c ? "a is greatest" : "c is greatest" : b > c ? "b is greatest" : "c is greatest";
System.out.println(result);
}
}
System.out.println(result);
}
}

Output:

c is greatest

Here you can see how instead of writing bulky codes, you can simply write one-liner codes by using nested operator and obtain the desired result.

With this, we have reached the end of this article. I hope the content explained above added value to your Java knowledge.

If you found this article on “Conditional Operator in Java” relevant, check out the Edureka Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Conditional Operator in Java” and our team will be glad to answer.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.