Assignment operators
Increment variable by right hand value:
int a = 4;
a = a + 2;
System.out.println("Value:" + a); |
int a = 4;
a += 2;
System.out.println("Value:" + a); |
Value:6 |
Logical and
operation:
boolean examSuccess = true,
registered = false;
examSuccess = examSuccess & registered;
System.out.println(
"Exam success:" + examSuccess); |
boolean examSuccess = true,
registered = false;
examSuccess &= registered;
System.out.println(
"Exam success:" + examSuccess); |
Exam success:false |
= |
Assign right to left operand |
+= |
Assign sum of operands to left operand |
-= |
Assign difference of operands to left operand |
*= |
Assign product of operands to left operand |
/= |
Assign quotient of operands to left operand |
%= |
Assign remainder of operands to left operand |
&= |
Assign logical “and” of operands to left operand |
|= |
Assign logical “or” of operands to left operand |
No. 51
Understanding +=
Q: |
Consider the following snippet:
This will compile and execute thereby incrementing the
On the other hand the
So why is |
||||
A: |
The Java® Language Specification SE 19 Edition offers a definition in its Compound Assignment Operators section:
We provide an example illustrating this rather condensed statement: double d = 4.5; byte i = 3; i += d ; // E1 op= E2 We thus link:
Our variable
Back to our original example: According to Figure 119, “No binary + operator yielding
NoteSince
On contrary regarding the
Notice 24 being equal to |