aspose file tools*
The moose likes Beginning Java and the fly likes what does return -1 mean ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what does return -1 mean ?" Watch "what does return -1 mean ?" New topic
Author

what does return -1 mean ?

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
I have seen this number of times. A methos is returning 0, 1 or -1.What actually does it mean when it says return -1 in this method OR any other number ?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

It means the author of the method does not appreciate exceptions properly. They are probably used to programming in a language like C, where clients of a method are usually notified of errors through some special return value. In higher programming language we use exceptions instead.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39837
    
  28
It also looks peculiar to use an InputStreamReader on an ASCII file rather than a FileReader.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39837
    
  28
. . . and since 2004, it has been better to use a Scanner.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14348
    
  22

This code will not even compile, because class BufferedReader does not have a constructor that takes a Reader and a String, as you are trying to use in line 4.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
so what will -1 do ?

Does it mean something special ? What if 0 or 1 is returned ? I think I had seen 0 and 1 being returned (I am not sure)

Can any value be returned (2, 3 4,...........) OR it has to be any one from -1, 0 or 1 only ? If so, why ?

I am just trying to figure out use of returned value and have copied code from somewhere. I have not run it.

Thanks
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4466
    
    8

nirjari patel wrote:so what will -1 do ?

That depends entirely on the code that calls this one. All this does is return it.

This method can return any integer. It tries to read an integer from a file. That could contain 0, 1, -284, 8263, etc. If there's a problem reading the number it will return -1 (but it will also return -1 if the file actually contains "-1").
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11479
    
  16

Back in the 'old days', you would often use a return value of a method to tell you if things worked. Often 0 or greater would mean things were OK, and specific values might tell you more specific things.

a negative value meant something went horribly wrong, and again, the specific value could tell you what that was. This required you to KNOW what those values mean, so hopefully the person who wrote the function you are calling provided good documentation...and that the function hadn't changed.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
That depends entirely on the code that calls this one. All this does is return it.

That means I can put any number in here and if file does not return number, then number I have specified will be returned.

Often 0 or greater would mean things were OK, and specific values might tell you more specific things.
a negative value meant something went horribly wrong, and again, the specific value could tell you what that was

"Providing 0 or greater , when things are ok and providing negative value, when things are wrong" , is this convention or is it syntax ?

Thanks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39837
    
  28
nirjari patel wrote: . . . "Providing 0 or greater , when things are ok and providing negative value, when things are wrong" , is this convention or is it syntax ?

Thanks
Convention.
There is another convention, still used by System.exit() that 0 means all well and any other value means things are wrong.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

What really matters is that the method must work as documented. If the documentation says that the method should return -1 to signal an error condition, that does usually indicate a poor design decision, but the method is bound to adhere to the spec.

There are several situations where returning -1 or other negative value is justifiably used to signal a singular non-error state/condition. Examples: String#indexOf / lastIndexOf, DefaultSingleSelectionModel#getSelectedIndex, Image#getWidth / getHeight, GraphicsDevice#getAvailableAcceleratedMemory

luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what does return -1 mean ?