Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what does return -1 mean ?

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5912
65
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It also looks peculiar to use an InputStreamReader on an ASCII file rather than a FileReader.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and since 2004, it has been better to use a Scanner.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12147
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic