views:

394

answers:

7

I can't search for | in Google. If you had found it in a software source code that you are trying to interpret, you didn't know what it does and you couldn't ask other people for help, how would you find out what it does?

+28  A: 

The pipe operator in this case means "use both SWT.APPLICATION_MODAL and SWT.OK as options/flags for my popup box." It's a very commonly used idiom with bitfield configuration identifiers, esp. in windowing systems like SWT or Win32.

How it works

The pipe (|) operator is the bitwise OR operator, that is, it computes an OR operation of the two binary integer values. If you check out where APPLICATION_MODAL and OK are defined, you'll find they are something like this:

...
SWT.OK = 1,                  // 00000001 in binary
SWT.ABORT_RETRY_IGNORE = 2,  // 00000010 in binary
SWT.OK_CANCEL = 4;           // 00000100 in binary
...
SWT.APPLICATION_MODAL = 32;  // 00100000 in binary
... (and so on...)

When you bitwise OR two (or more) of these numbers together, individual bits will be set for each of the options:

int style = SWT.OK | SWT.APPLICATION_MODAL = 00000001 | 00100000 = 00100001

The windowing toolkit that goes to interpret style will be able to tell exactly what you wanted (a popup box that is Modal and has an OK button) by doing a bitwise AND like this:

...
if(style & SWT.OK)
{
    // we want an OK box
}
if(style & SWT.ABORT_RETRY_IGNORE)
{
    // we want an Abort/Retry/Ignore box
}
if(style & SWT.OK_CANCEL)
{
    // we want an OK/Cancel box
}
...
if(style & SWT.APPLICATION_MODAL)
{
    // We want a modal box
}
...

Kinda clever, in my humble opinion. It allows you to select/represent multiple configuration options in a single variable. The trick is in the integer definitions of the options, and ensuring that they are only powers of 2.

sheepsimulator
Very detailed answer. +1
MitMaro
You may also want to compare this answer for more info: http://stackoverflow.com/questions/276706/what-are-bitwise-operators/276771#276771
sheepsimulator
This is very common in C and C++, but I'm curious (as someone who hasn't done much Java) as to whether it's idiomatic in Java to use an integer as a set of boolean flags?
caf
Not that common in Java, no. You see it occasionally in low-level APIs but I wouldn't describe it as common. A more 'Java-ey' way of doing this, since Java 5 at least, is to declare them as an enum and then use something like EnumSet.of(SWTOptions.OK, SWTOptions.ApplicationModel) (EnumSet is implemented as a 64-bit long bitmask anyway, behind the scenes, or a just-long-enough array of longs if there are more than 64 elements in the enum)
Cowan
+1  A: 

| is the OR operator. Those two values are probably flags that are ints with 1 bit set, and the resulting value is which style values to use.

Nathaniel Flath
A: 

Bitwise OR.

With most things, there's a bit of a learning curve. If I was learning a new language, I would assume that | was an operator, and would search for 'Java operators'.

Charlie Salts
+1  A: 

It is a bitwise OR, it provides a means for you to pass a number of flags to the SWT component as a single int, rather than having to overload the type with loads of setter methods.

The two properties you list indicate you have a dialog or other window that should be displayed modally over the parent shell, and use the ok button. You could combine it with SWT.CANCEL to display an OK and Cancel button for instance

Rich Seller
+10  A: 

In answer to your question about finding out what it does, I would first reason to myself whether it was a symbol or an operator. That alone helps me figure out what to search for in Google. The types of the variables being used also give me a clue - it's an operator that's working on int types.

Hope that helps.

weiji
thanks for reading the part that most people skipped!!!
Delirium tremens
thanks for letting me know you appreciate it! It seems like lots of people are quick to post since they want to be the first to answer and get rep points fast... oh well.
weiji
A: 

Use a search engine that can handle it: A | B

rodrigoap
Or a search engine which doesn't answer the question. The OP wanted to know what it means *in Java*. Not in mathematics in general.
jalf
@jalf, relax man.
rodrigoap
A: 

Unfortunately, Google is lousy at symbols. Instead, I'd start by looking at the Wikipedia page, and search on there for "|", which is at:

and that page has a pretty good overview of what's what and how to use it.

Ken