• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

method return style

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the condition (current != null) is true, then the method returns true, otherwise it returns false.

Regards,
JD
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic