Java Operators - ITC101.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

JAVA OPERATORS – ITC101 Sure! Let’s dive into the Java operators, focusing on **Arithmetic**, **Logical**, and **Relational** operators. These are essential building blocks in Java programming, and understanding them thoroughly is key to writing effective Java code. ### **1. Arithmetic Operators**...

JAVA OPERATORS – ITC101 Sure! Let’s dive into the Java operators, focusing on **Arithmetic**, **Logical**, and **Relational** operators. These are essential building blocks in Java programming, and understanding them thoroughly is key to writing effective Java code. ### **1. Arithmetic Operators** Arithmetic operators in Java are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and modulus. Here’s a breakdown of each: #### **Basic Arithmetic Operators** | Operator | Name | Example | Explanation | |----------|-------------|---------------|----------------------------------| | `+` | Addition | `a + b` | Adds two operands (`a` and `b`). | | `-` | Subtraction | `a - b` | Subtracts `b` from `a`. | | `*` | Multiplication| `a * b` | Multiplies two operands. | | `/` | Division | `a / b` | Divides `a` by `b`. | | `%` | Modulus | `a % b` | Remainder when `a` is divided by `b`. | #### **Key Points:** - **Division `/`:** When dividing two integers, the result is an integer (fractional part is discarded). To get a floating-point result, at least one of the operands must be a float or double (e.g., `10 / 3 = 3`, but `10.0 / 3 = 3.3333`). - **Modulus `%`:** This operator returns the remainder of a division. For example, `10 % 3 = 1`, since 3 goes into 10 three times (9) and leaves a remainder of 1. #### **Increment/Decrement Operators** | Operator | Name | Example | Explanation | |----------|----------------|-----------|--------------------------------| | `++` | Increment | `a++` | Increases `a` by 1 (post-increment).| | `--` | Decrement | `a--` | Decreases `a` by 1 (post-decrement).| JAVA OPERATORS – ITC101 | `++` | Pre-Increment | `++a` | Increases `a` by 1 before evaluation.| | `--` | Pre-Decrement | `--a` | Decreases `a` by 1 before evaluation.| #### **Example:** ```java int x = 10; int y = x++; // y = 10, x = 11 (post-increment: x is incremented after assignment) int z = ++x; // z = 12, x = 12 (pre-increment: x is incremented before assignment) ``` ### **2. Relational Operators** Relational operators are used to compare two values or expressions and return a boolean result (`true` or `false`). These are crucial for decision-making and control flow in Java programs, especially in conditional statements like `if`, `while`, and `for`. | Operator | Name | Example | Explanation | |----------|-------------------|------------|----------------------------------| | `==` | Equal to | `a == b` | True if `a` equals `b`. | | `!=` | Not equal to | `a != b` | True if `a` is not equal to `b`. | | `>` | Greater than | `a > b` | True if `a` is greater than `b`. | | `=` | Greater than or equal to| `a >= b`| True if `a` is greater than or equal to `b`. | | ` b) { System.out.println("a is greater than b"); } else { System.out.println("a is not greater than b"); } ``` ### **3. Logical Operators** Logical operators in Java are used to perform logical operations, primarily for combining multiple conditions. They operate on boolean values and return a boolean result. The main logical operators are **AND**, **OR**, and **NOT**. #### **Logical AND (`&&`)** - **Description:** Returns `true` if both conditions are `true`. - **Example:** ```java if (a > 0 && b > 0) { System.out.println("Both a and b are positive."); JAVA OPERATORS – ITC101 } ``` - **Explanation:** The condition evaluates to `true` only if both `a > 0` and `b > 0` are true. #### **Logical OR (`||`)** - **Description:** Returns `true` if at least one condition is `true`. - **Example:** ```java if (a > 0 || b > 0) { System.out.println("At least one of a or b is positive."); } ``` - **Explanation:** The condition evaluates to `true` if either `a > 0` or `b > 0` is true. #### **Logical NOT (`!`)** - **Description:** Reverses the logical state of its operand. If a condition is `true`, `!` makes it `false`, and vice versa. - **Example:** ```java if (!(a > 0)) { System.out.println("a is not positive."); } ``` - **Explanation:** If `a > 0` is `true`, then `!(a > 0)` will be `false`. #### **Logical Operator Truth Table** | `a` | `b` | `a && b` | `a || b` | `!a` | |--------|--------|----------|----------|--------| JAVA OPERATORS – ITC101 | true | true | true | true | false | | true | false | false | true | false | | false | true | false | true | true | | false | false | false | false | true | #### **Short-circuit Evaluation:** - Java supports **short-circuiting** with logical operators. This means that evaluation of a logical expression stops as soon as the outcome is determined: - For `&&`, if the first condition is `false`, the whole expression is `false`, so the second condition is not evaluated. - For `||`, if the first condition is `true`, the whole expression is `true`, so the second condition is not evaluated. #### **Example of Short-Circuit:** ```java int a = 10; if (a > 5 || a++ > 20) { System.out.println("a: " + a); // a remains 10, because a > 5 is true, so a++ is not evaluated. } ``` ### **Combining Relational and Logical Operators:** Relational and logical operators often work together to form complex conditional statements in programs. #### **Example:** ```java int a = 10; int b = 20; JAVA OPERATORS – ITC101 if (a < 15 && b > 15) { System.out.println("a is less than 15 and b is greater than 15"); } ``` In this case: - `a < 15` is `true`. - `b > 15` is `true`. - The `&&` operator ensures both conditions are true, so the statement inside the `if` block will execute. ### **Operator Precedence** Java evaluates operators based on a predefined **operator precedence**. Operators with higher precedence are evaluated first. For example, arithmetic operators generally have higher precedence than relational operators, which in turn have higher precedence than logical operators. #### **Example of Precedence:** ```java int result = 5 + 3 * 2; // 5 + (3 * 2) = 5 + 6 = 11 ``` In this case, multiplication `*` is performed before addition `+`. To explicitly control the order of operations, you can use parentheses `()`. #### **Example with Parentheses:** ```java int result = (5 + 3) * 2; // (5 + 3) * 2 = 8 * 2 = 16 ``` JAVA OPERATORS – ITC101 ### **Conclusion** In summary, Java operators form the foundation of many programming constructs. **Arithmetic operators** handle mathematical computations, **Relational operators** compare values and return boolean outcomes, and **Logical operators** combine multiple conditions into complex decision-making logic. Understanding how these operators interact, especially in terms of precedence and short-circuit evaluation, is essential for writing clear and efficient Java code.

Use Quizgecko on...
Browser
Browser