If you don't mind having the scope of the object variable being confined to the code in the curly brackets, then you can do this:
for (Object object; (object = queue.poll()) != null;) {
// do whatever with object
break;
}
that one isn't too far off from the typical thing we do when reading a file, like this:
for (String line; (line = reader.readLine()) != null;) {
// do something with line
}
or alternatively as suggested by rzwitserloot:
for (Object object = queue.poll; object != null; object = null) {
// do something with object
}
that way it polls only once, and omitting a break in the body of the loop does no harm. Nulling out the object in the increment makes sure the loop terminates after the first pass.
It is kind of abusive having a for loop where you have no intention of looping. I would stay with the multi-line version you started with.
But if you are polling, usually you need a loop anyway.
Object object = queue.poll(); if (object != null) { whatever }- Java does not require line feedsifblockif((Object object = queue.poll()) != null) { ... }?