What does "| =" mean in Java?

Please note that my question is not relevant !=, but|=

Usage example here

I assume it x |= ymatches x = x | y, but I could not find supporting documentation and wanted to be sure

thank

+3
source share
4 answers

Yes, this is a bitwise inclusion or assignment: http://www.cafeaulait.org/course/week2/03.html

+4
source

This is a bitwise "or" plus an assignment, so you are absolutely correct in your assumption.

+7
source

, x | = y x = x | ().

Here is an interesting example of why this is important.

int c = 2;
c %= c++ * ++c;

An interesting consequence here is that it will be written as

c = c % (c++ * ++c);

The Java specifications tell us that the JVM will first see the initial c and save it, everything that preceded it will not affect it, so C ++ and ++ c will not actually affect the result of the calculation. It will always be c = 2%, which is 2 :)

+3
source
+2
source

Source: https://habr.com/ru/post/1758319/


All Articles