image
The Ternary Operator
The Ternary Operator
Difficulty
Beginner
Duration
3h 15m
Students
118
Ratings
4.6/5
Description

In this course, we look at control flow statements in the Java programming language. 

Learning Objectives

  • Decision-making statements
  • Looping statements
  • Branching statements 

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language.
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll learn about the Ternary Operator. If you remember, we mentioned the ternary operator in the operator section, but in this lesson we'll take a closer look at the ternary operator. Let's first look at what the ternary operator is. This operator is the conditional operator and it's represented by the symbol, ?:.

This operator consists of three operands and is used to evaluate Boolean expressions. This operator can be thought of as shorthand for an if-else statement. Thanks to the ternary operator, we can make a simple conditional statement in one line. Let's compare it with the if-else statement for better understanding. You see the if-else statement on the slide. If the condition checked in the parentheses was true, the codes in the if block were executing. If the condition is false, the code in the else block executes. Now let's look at the equivalent of this if-else statement in the ternary operator. First, the condition is checked. If the condition is true, the statement after the question mark works. 

If the condition is false, the statement after the colon works. Let's make some examples with the ternary operator. First, let's fix the perspective in Eclipse. If you remember, we used the breakpoint in the previous lesson and the perspective was changed. Let's fix this now. You can see the perspective icons in the top right corner of Eclipse. If we click on this icon, we can switch to the Java perspective. If we click on the debug icon, we can switch to the debug perspective. Now let's move on to the Java perspective. Yes, we can continue now. In exercise project, right click on the decision-making package and select new class. Specify the class name as TernaryOperator and select the checkbox for the main method. Okay, let's start to code. First, I'll create two integer variables int a = 20 and int result. But I will not initialize the result variable, I will initialize it soon.

Now let's create an if-else statement first. If( a > 10){ the result = a/5; }else the result = a/4; And outside the if-else block, I'll print the result to the console. System.out.println("Result = " + results); Now let's examine the code. The value of a is 20, so 20 is greater than 10. So, the condition is true. For this reason the if block will be executed. So, the result will be 4 because 20/5 is 4. Let's run the code. As you can see, the result is 4. Now let's try writing this with the ternary operator. First, I will convert that if-else statement to the comment line. Okay, let's write the code. results =, after the equals, first, I will write the condition, so a > 10 ?  and I will put a question mark. 

And now, I'll write the code inside the if block. So, the code is a/5, and I'll put a colon. And lastly, I'll write the code in the else block. Now let's examine the code. First, it will check the condition. So, the condition is a i.e. 20 > 10. So, the condition is true. Since the condition is true, the code after the question mark will be executed and the result will be assigned to the result variable. So, the result variable will be 4. Let's run and see. As you can see, the result is 4. Now let's change the condition. This time I'll use the less than operator. So, the value of a variable is 20, and 20 is greater than 10, so the condition is false. Since the condition is false, this time, the code after the colon will be executed. So, the result will be 5 because 20/4 is 5. Now, let's run and see. As you can see, the result is 5. Now let's take another example. I'll create two int variables. 

First, x and assign 6. Second, y and assign 5. Now, let's create a condition with the ternary operator. Int z = x == y ? x-- : y-- ; and lastly, let's print these variables. System.out.println(" x = " + x); I'll copy this print method and paste it twice. This will be y and this will be z. Now let's examine the code. First, it will check the condition. So, the x is not equal to y because 6 is not equal to 5. So, the condition is false. Since the condition is false, the code after the colon will be executed. So first, the value of the x will remain the same, x is 6. And this is the decrement operator. It'll work after the value y is assigned to the z. So, the value of the z is 5. And lastly, the value of the y will decrease by 1, so y will be 4. Let's run and see. As you can see, x is 6, y is 4, and z is 5. Yes friends, I think you understand the ternary operator. Let's take a short break here. We'll continue in the next video. See you in the next video.

 

About the Author
Students
3995
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics