Sep 02, 2019 Introduction To Java, Java Tutorial comments
Many times when we want to assign an expression to a given variable, the expression uses simple operation that involves the original value of the variable. For example, a = a + 2 means we want to add 2 to the variable, and assign the value back to it. Java provides a short hand notation for this to make programming more concise and easier to understand. We call it compound assignment operators. We will discuss around 11 of these in this post. Below are examples on how to perform compound assignment operators in Java.
Compound Assignment Operators
Below are the Compound Assignment Operators that we will typically use in our programs.
- += increment assign
- -= decrement assign
- *= multiply assign
- /= divide assign
- %= modulus assign
- <<= binary left shift assign
- >>= binary right shift assign
- >>>= binary right shift zero fill assign
- &= binary AND assign
- ^= binary exclusive OR assign
- |= binary inclusive OR assign
Increment Assign
The operator denoted by
+= will perform increment assign operation. What this operation mean is to add a value to the left hand variable and assign it back to the variable. For example:
int a = 5;
a += 3;
System.out.println(a);
The variable has an initial value of 5 and then we perform the compound assignment operator increment assign. So 3 was added to 5 and assigned back the value to a. Below is the output of the code above:
8
Decrement Assign
The operator denoted by
-= will perform decrement assign operation. This symbol will instruct the virtual machine to start with the value of the variable and decrement the succeeding expression, and assign back the result to the given variable. This is very similar to the first operator above. For example:
int a = 5;
a -= 3;
System.out.println(a);
So we start with a having the value of five and then 3 was deducted to it. The result, which is 2, is assigned back to the variable a. hence we get below result:
2
Multiply Assign
The operator denoted by
*= will perform multiply assign operation. Again, similar to examples above, multiply assign will multiply the value of the variable with the expression on the right. The result is assigned back to the variable. It is the combination of using the original value against the given expression that made this compound operation. Meaning we compress two operation into one. See below:
int a = 6;
a *= 4;
System.out.println(a);
So similarly, tracing the code above means we start a with a value of 6. Then a is multiplied with 4, that will result to 24. Then this value is assigned back to a. hence when we inspect the value of a, we get:
24
Divide Assign
The operator denoted by
/= will perform divide assign operation. So since this is a compound statement, we compress two operation, which is divide the left operand with the right value, and assign it back to the variable:
int a = 15;
a /= 5;
System.out.println(a);
Tracing the code above, a have initial value of 15, then divided by 5, and the quotient is assigned back to a. Hence the output is shown below:
3
Modulus Assign
The operator denoted by
%= will perform modulus assign operation. We assume you are familiar with the modulus operation. So the left side is divided with the expression on the right, and the remainder is assigned to the variable again. See below example:
int a = 16;
a %= 5;
System.out.println(a);
So a, which has a value of 16, is divided by 5, and then we take the remainder. The remainder of 16 divided by 5 is 1, which will be the new value of a:
1
Binary left shift Assign
The operator denoted by
<<= will perform binary left shift assign operation. Left shift is shifting the bits of a value to the left. The result is something like multiplying the value by the power of 2. For example, shifting by 1 will multiply by 2, shifting by 2 will multiply by 4, shifting by 3 will multiply by 8, and so on. Below is a simple example:
int a = 1;
a <<= 5;
System.out.println(a);
So we shift 1 by 5, which is similar to multiplying it by 2 to the power of 5. Which will result to:
32
Binary right shift Assign
The operator denoted by
>>= will perform binary right shift assign operation. This is the opposite of the left shift. Right shift is like dividing a value by some power of two. So right shifting by 1 is like dividing by 2, shifting by 2 is like dividing by 4, shifting by 3 is like dividing by 8. See below example:
int a = 128;
a >>= 5;
System.out.println(a);
The above code is like dividing 128 by 2 to the power of 5, which is 128 divided by 32, which will result to:
4
Binary right shift zero fill Assign
The operator denoted by
>>>= will perform binary right shift zero fill assign operation. The difference between binary right shift and binary right shift zero fill is that the former is signed, while the latter is unsigned. But the result is similar if we are performing it on positive numbers. For example:
int a = 256;
a >>>= 5;
System.out.println(a);
So we are shifting 256 to the right by 5 digits. This will result to the value below:
8
Binary AND Assign
The operator denoted by
&= will perform binary AND assign operation. What this do is perform a bitwise AND operation on the left and right side and assign back the value to the variable:
int a = 14;
a &= 5;
System.out.println(a);
In the given example, we are matching the digits from 14 and 5, and there is only one common digit, which is the third from the right, that represents the number 4:
4
Binary exclusive OR Assign
The operator denoted by
^= will perform binary exclusive OR assign operation. This operation will perform an XOR on the variable with the given expression, and assign back to the variable. See below example:
int a = 14;
a ^= 5;
System.out.println(a);
So the XOR of 14 and 5 will result to 8 + 2 + 1, which is 11. Hence the below output:
11
Binary inclusive OR Assign
The operator denoted by
|= will perform binary inclusive OR assign operation. The inclusive OR will usually result in a bigger number than the operand. This operation will perform inclusive OR with the variable and the given expression, and assign it back again to the variable. See below:
int a = 14;
a |= 5;
System.out.println(a);
Which is in contrast with XOR. This inclusive OR will result to 8 + 4 + 2 + 1, which is 15. See below output of the code:
15
Introduction to Java Topics