-1

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

Héctor van den Boorn
  • 1,218
  • 13
  • 32

4 Answers4

2

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.

AdamSpurgin
  • 951
  • 2
  • 8
  • 28
  • Useful answer to me. But why is it necessary to do a bitwise inclusive to maximize a JFrame? I tested this method just using JFrame.MAXIMIZED_BOTH and it maximizes my JFrame. – CompSci-PVT Sep 04 '13 at 09:47
  • `f.getExtendedState()` returns the current state of that frame so doing that bitwise operation maximizes it without disrupting the existing state. Using just `JFrame.MAXIMIZED_BOTH` would reset any other attribute in the bitfield. – AdamSpurgin Sep 16 '13 at 16:08
0

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.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

| stands for bitwise inclusive OR operater. Check for details here:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/opsummary.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

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).

feralin
  • 3,268
  • 3
  • 21
  • 37