Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes return -1. why ??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "return -1. why ???" Watch "return -1. why ???" New topic
Author

return -1. why ???

saravanan ks
Greenhorn

Joined: Jul 10, 2003
Posts: 9
Dear friends:
In while using If..else statements......if the any condition fails we are returining the statment like "return false" but in some case i have seen that peoples are using "return -1".
Please help me..
Thanks in advance.
Best Regards
K S Saravanan
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by saravanan ks:
Dear friends:
In while using If..else statements......if the any condition fails we are returining the statment like "return false" but in some case i have seen that peoples are using "return -1".
Please help me..
Thanks in advance.
Best Regards
K S Saravanan

Hi saravanan, and welcome to the ranch. I guess the answer to your question depends on the return type of the method in which the if-stat is located in (if it is of type int or boolean).
If you can post the question then maybe we'll be able to help more.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
saravanan ks
Greenhorn

Joined: Jul 10, 2003
Posts: 9
Dear Vicken Karaoghlanian :
Thanks for immediatate response,
In the methods, return type is like this..

Thanks
K S Saravanan
[ December 30, 2003: Message edited by: Michael Ernest ]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

A return of -1 is consistent with a failed system call, or in the case of Java, failure in receiving response from a stream which operates on serialized values. Since all byte streams are interpreted initially as integer values, -1 is used to express an improperly functioning or non-function one.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Returning "magic numbers" to indicate failure is a debatable practice. Sun used -1 in many places, but not everywhere. Sometimes any integer would be a valid result, so a special integer for error cannot work and they throw an exception. You also get into the whole question of what's a failure and what's normal operation. Reading from a stream returns -1 at end because that's normal ... every stream eventually comes to an end. Reading from a stream that's not open gets an exception because that's a serious error.
In your example it might be appropriate (and much easier to read) to throw a variety of exceptions. See how this gets rid of a lot if if/else clauses:

This gets even cleaner if we throw the exceptions in called methods that do the details, like allocateMemory(). Then this code might look like:

Hope that made sense and gave you some ideas!
[ December 30, 2003: Message edited by: Stan James ]

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: return -1. why ???
 
Similar Threads
html:radio
Return -1
finding a particular string in string type
Comparable interface
Trouble with Regex