• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method return style

 
Udayan Kumar
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Sorry if this question sounds naive.
Recently I saw a piece of code something like this
-----------------------------------------
/**
* Test if the current position is a valid position in the list.
* @return true if the current position is valid.
*/
public boolean isValid( ) {
return current != null;
}
ListNode current;
------------------------------------------
the statement (return current != null) is something I did not get convincingly. How does this statement evaluate & work.


-Uday
 
John Dell'Oso
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the condition (current != null) is true, then the method returns true, otherwise it returns false.

Regards,
JD
 
Bob Ruth
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a way to "peel it apart"....

look at the method declaration.... it says that the method returns boolean.

That means that, when the method exits it needs to return a boolean value, either true or false.

Now if you look at everything after the return, it is an expression that makes a comparison that is either going to be true or false, based on the specified test.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Udayan Kumar:
Hi All,

Sorry if this question sounds naive.
Recently I saw a piece of code something like this
-----------------------------------------
/**
* Test if the current position is a valid position in the list.
* @return true if the current position is valid.
*/
public boolean isValid( ) {
return current != null;
}
ListNode current;
------------------------------------------
the statement (return current != null) is something I did not get convincingly. How does this statement evaluate & work.


-Uday


It's the same as:

 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's also the same as

With so many ways to say the same thing, it's no surprise that it can be confusing. Do you see the common bit in every one of these that the tiny expression "current != null" can be replaced by true or false just about anywhere it is used? And that means you can use an expression just about any place you need true or false.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic