Monday 23 March 2020

What is |= stands for in Java?

|= is a compound assignment operator.

a|=10 evaluates to a = a | 10

Below table summarizes the compound assignment operators.
Compound Operator
Example
Explanation
+=
a+=3
a =  a + 3
-=
a-=3
a =  a – 3
*=
a*=3
a =  a * 3
/=
a/=3
a =  a / 3
%=
a%=3
a =  a % 3
&=
a&=3
a =  a & 3
|=
a|=3
a =  a | 3
^=
a^=3
a =  a ^ 3
>>=
a>>=3
a =  a >> 3
>>>=
a>>>=3
a =  a >>> 3
<<=
a<<=3
a =  a << 3

App.java
package com.sample.app;

public class App {

 public static void main(String args[]) {
  int a = 7;

  System.out.println("(a|=3) = " + (a |= 3));
  System.out.println("(a+=3) = " + (a += 3));
  System.out.println("(a-=3) = " + (a -= 3));
  System.out.println("(a*=3) = " + (a *= 3));
  System.out.println("(a/=3) = " + (a /= 3));
  System.out.println("(a%=3) = " + (a %= 3));
  System.out.println("(a&=3) = " + (a &= 3));
  System.out.println("(a^=3) = " + (a ^= 3));
  System.out.println("(a<<=3) = " + (a <<= 6));
  System.out.println("(a>>=3) = " + (a >>= 3));
  System.out.println("(a>>>=3) = " + (a >>>= 3));

 }

}

Output

(a|=3) = 7
(a+=3) = 10
(a-=3) = 7
(a*=3) = 21
(a/=3) = 7
(a%=3) = 1
(a&=3) = 1
(a^=3) = 2
(a<<=3) = 128
(a>>=3) = 16
(a>>>=3) = 2



You may like

No comments:

Post a Comment