Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

return - statement I don't understand

 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
what does this mean:
return !hasMoreJobs () ? null : jobs [jobIndex++];
Thanks.
Thomas
 
Marcel de Jong
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,

can also be written als

Using the first notation, is considerable shorter.
Marcel
[ August 16, 2002: Message edited by: Marcel de Jong ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
Dave
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you keep the ternery operator, ditching the negation is more readable style:
return hasMoreJobs () ? jobs [jobIndex++] : null;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic