Types of Operators (Augmented assignment, Increment and Decrement, Logical)
- Get link
- X
- Other Apps
Types of Operators :
- Augmented Assignment Operators:
Augmented operators, also known as compound assignment operators, allow you to combine arithmetic operators with the assignment operator in a concise way. Instead of writing separate statements to perform an operation and assign the result back to a variable, you can use augmented operators to achieve the same result in a more compact form.
For example, let's consider the statement `count = count + 1;`. This statement increases the value of the variable `count` by 1. In Java, you can use the addition assignment operator (`+=`) to achieve the same result. So, the statement can be written as `count += 1;`.
The `+=` operator is an example of an augmented assignment operator. It combines the addition operator (`+`) with the assignment operator (`=`). This operator adds the value on the right side of the operator to the current value of the variable on the left side, and then assigns the result back to the variable.
Java provides several other augmented assignment operators, as shown in Table-1:
These operators can be used with arithmetic operations such as addition, subtraction, multiplication, division, and remainder. They are useful when you want to update the value of a variable by performing an operation on its current value and assigning the result back to the same variable. Using augmented operators can make your code more concise and readable.
The augmented assignment operator is performed last after all the other operators in the expression are evaluated. For example, x /= 4 + 5.5 * 1.5;
is same as x = x / (4 + 5.5 * 1.5);
- Increment and Decrement Operators:
The increment operator (`++`) and decrement operator (`--`) are used to increase and decrease the value of a variable by 1, respectively.
These operators provide a shorthand way to increment and decrement a variable by 1, which is a common operation in many programming tasks. For example, consider the following code:
- int i = 3, j = 3;
- i++; // i becomes 4
- j--; // j becomes 2
In this code, `i++` is pronounced as "i plus plus" and `j--` is pronounced as "j minus minus." These operators are called postfix increment and postfix decrement because the `++` and `--` operators are placed after the variable. They can also be placed before the variable, resulting in a prefix increment (`++i`) and prefix decrement (`--j`), respectively.
So, in the code snippet:
- int i = 3, j = 3;
- ++i; // i becomes 4
- --j; // j becomes 2
Here, `++i` increments `i` by 1 and `--j` decrements `j` by 1. These operators are known as prefix increment and prefix decrement.
The effects of `i++` and `++i`, or `j--` and `--j`, are the same in the examples mentioned above. However, their effects can differ when they are used in statements that do more than just incrementing or decrementing a variable. Table 2.5 describes the differences between them and provides examples to illustrate their behavior.
- Logical:
Logical operators in Java are used to perform logical operations on boolean values. There are three logical operators in Java: `&&` (logical AND), `||` (logical OR), and `!` (logical NOT).
1. Logical AND (`&&`): The logical AND operator returns `true` if both operands are `true`, and `false` otherwise. It evaluates the second operand only if the first operand is `true`. Here's the truth table for the logical AND operator:
2. Logical OR (`||`): The logical OR operator returns `true` if at least one of the operands is `true`, and `false` otherwise. It evaluates the second operand only if the first operand is `false`. Here's the truth table for the logical OR operator:
3. Logical NOT (`!`): The logical NOT operator is a unary operator that negates the value of its operand. If the operand is `true`, the operator returns `false`, and if the operand is `false`, the operator returns `true`.
Here's an example code snippet demonstrating the use of logical operators:
- boolean a = true;
- boolean b = false;
- boolean result1 = a && b; // Logical AND
- boolean result2 = a || b; // Logical OR
- boolean result3 = !a; // Logical NOT
- System.out.println(result1); // Output: false
- System.out.println(result2); // Output: true
- System.out.println(result3); // Output: false
In this example, `result1` is `false` because `a` is `true` and `b` is `false`, `result2` is `true` because `a` is `true`, and `result3` is `false` because `a` is `true` and `!` negates it.
- Get link
- X
- Other Apps
Comments
Post a Comment