wood burning stoves
The moose likes Beginning Java and the fly likes return - statement I don't understand Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return - statement I don Watch "return - statement I don New topic

return - statement I don't understand

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
what does this mean:
return !hasMoreJobs () ? null : jobs [jobIndex++];
Marcel de Jong
Ranch Hand

Joined: May 27, 2002
Posts: 54

can also be written als

Using the first notation, is considerable shorter.
[ August 16, 2002: Message edited by: Marcel de Jong ]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Or to paraphrase to a slightly more readable version:

No difference, just relies on the fact that things are easier to understand if you don't have to get your head around a negation first...
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
booleanTest ? expression1 : expression2
This is the only ternary operator in Java. As already expressed, it's a shorthand if-else construct. If the boolean test is true, then the first expression is evaluated and returned; otherwise the second expression is evaluated and returned.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Even if you keep the ternery operator, ditching the negation is more readable style:
return hasMoreJobs () ? jobs [jobIndex++] : null;

Ron Newman - SCJP 1.2 (100%, 7 August 2002)
I agree. Here's the link: http://aspose.com/file-tools
subject: return - statement I don't understand
It's not a secret anymore!