I saw recently in a code example the following:
f.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH );
where f is a JFrame. How is this pipe operator called, what does it do and where can I find documentation about it?
Thank you Héctor
I saw recently in a code example the following:
f.setExtendedState( f.getExtendedState()|JFrame.MAXIMIZED_BOTH );
where f is a JFrame. How is this pipe operator called, what does it do and where can I find documentation about it?
Thank you Héctor
That 'pipe' is actually a bitwise inclusive or. f.getExtendedState() and JFrame.MAXIMIZED_BOTH are probably number indexes in bitfields. using the 'or' operator combines the properties of both into one value.
The | operator is the bitwise-or operator in Java.
The result of a bitwise-or is a value with bits set in it if the corresponding bit was set in either of the operands (or both).
Here, this operation uses the value of JFrame.MAXIMIZED_BOTH (in binary, 0000 0110) to ensure that the second to last and third to last bits are turned on, one for horizontal and one for vertical. This leaves all other bits from f.getExtendedState() intact.
| stands for bitwise inclusive OR operater. Check for details here:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html
The pipe (|) operator is simply bitwise-or operator. It will go through the respective bits of two numbers, and the resulting number will have an on bit where either of the two input bits were on. In the case you gave us, the operator is used to add a flag to a bitfield.
For example, if you have a number flags, which (let's say) is 4, it would look like
00000100b
in binary. If you | it with the number 00010000b (16), the result is
00010100b,
which contains the original flag (at bit position 3) and the new flag (at bit position 5).