The course is part of this learning path
In this course, we'll learn about Java data types and operators.
- Data Types
- Type Conversion & Type Casting
- Operator Precedence
- Expressions, Statements and Block
- 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
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there. In this video, we talk about Conditional Operators. The Conditional Operators are used for checking multiple conditions together. Let's try to understand Conditional Operators. Conditional AND Operator. This operator is represented by the symbol Double Ampersand, &&. In this operation, the result is true if both operands are true. This operator doesn't check the second condition if the first condition is false. It checks the second condition only if the first one is true. Conditional OR Operator. This operator is represented by the symbol, Double Pipe, ||. In this operation, the result is true if either of the operands is true. This operator doesn't check the second condition if the first condition is true. It checks the second condition only if the first one is false. Ternary Operator. This operator is represented by the symbol, Question Mark Colon, ? :. 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. We'll cover this in the Control Flow Statements section of this course. Let's make some examples with Conditional Operators.
In Exercise project, right-click on the operator package and select New, Class. Specify the class name as ConditionalOperator and select the check box for the main method. Okay, let's start to code. Let's declare three variables with int type. You may have heard it said I-N-T but I usually say int. Firstly, number1 and assign 5. Second, number2 and assign 8. And last, number3 and assign 12. Okay, let's declare a variable result with Boolean type. If you remember, the default value for Boolean data type is false. I will use the Conditional AND Operator, and assign the result to the variable result. If the number1 is greater than the number2, and the number3 is greater than the number1, the result will be true else the result will be false. In order to get the result true in the Conditional AND Operator, all Boolean expressions should be true. Let's print the result value by using the print method. Okay, let's run the code.
You see in the console, the Result = false because the number1 is not greater than the number2. Okay, now let's use the Conditional OR Operator and assign the result to the variable result. If the number1 is greater than the number2 or the number3 greater than the number2, in order to get the result true in the Conditional OR Operator, at least one expression must be true. Let's copy the print method. Okay, let's run the code. You see in the console, the Result = true because number3 is greater than number2. Let's look at the Ternary Operator. We use the Ternary Operator and assign the result to the variable result. If the number1 is less than the number2 ? true : false. So, if the condition is true, the first result, i.e., true will be assigned to the result variable. If this condition is false, this time the second result, i.e., false will be assigned to the result variable. Let's copy the print method and paste it here. Okay, let's run the code. The Result = true because the number1 is less than the number2. Excellent. Let's take a short break here. See you in the next video.
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.