| Author |
method return style
|
Udayan Kumar
Ranch Hand
Joined: Jan 16, 2007
Posts: 66
|
|
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
Joined: Apr 08, 2004
Posts: 130
|
|
If the condition (current != null) is true, then the method returns true, otherwise it returns false. Regards, JD
|
 |
Bob Ruth
Ranch Hand
Joined: Jun 04, 2007
Posts: 318
|
|
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.
|
------------------------
Bob
SCJP - 86% - June 11, 2009
|
 |
Manuel Leiria
Ranch Hand
Joined: Jul 13, 2007
Posts: 171
|
|
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:
|
Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
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.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: method return style
|
|
|