• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return -1

 
saravanan ks
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I learning java thro net, in some of samples I have seen peoples using "return -1" and "return 1" in if loops.
Can one clarify what it means please
Thanks
Saravanan
 
Mike Gershman
Ranch Hand
Posts: 1272
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what an "if loop" is.

Please post the code you saw and we'll try to help you.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a common problem for methods to indicate success or failure along with a return value. If you look at String.indexOf() it returns non-negative for a successful find and -1 for not found. And if you look at BufferedReader.readLine() it returns a valid string for good reads and null for end of file. So right from the JDK we have two solutions that are not consistent.

Yikes! How many more ways will we find to indicate some special condition? Quite a few, I'm afraid.

I'd suggest two lessons from this: read the JavaDoc closely on methods you're not familiar with, and try to follow some example in your own methods and document them well so they'll make sense to others. These two examples won't always work ... some times negative answers are valid, sometimes null strings are valid. Then I guess you could look into throwing exceptions.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic