• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Return type concept problem

 
Deepakk Verma
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if we have a method with return type boolean then is it compulsory to have both true and false returns like



or is it ok if i give only one return value as

 
Maneesh Godbole
Saloon Keeper
Posts: 10971
11
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did your code compile?
 
Janeice DelVecchio
Saloon Keeper
Posts: 1800
12
Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that the first way is better. I don't think it matters though, because JVM doesn't care WHAT you do (if anything) with the return value, just that when the method says it returns something that it does exactly that.

I'm not exactly sure, but if I wrote something similar, it would look like your first example.

Janeice
 
Deepakk Verma
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my first example is compiling but second one is not.......
it says

"This method must return a result of type boolean"
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepakk Verma wrote:my first example is compiling but second one is not.......
it says

"This method must return a result of type boolean"


JVM complains because, there is no way to know that your if condition will be true. If that is not true, you are not returning anything as against method expectaton to return boolean
 
Deepakk Verma
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok....now i got it
Thanks Mr.Rahul
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome
 
D. Ogranos
Ranch Hand
Posts: 214
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw. if you have such a condition, you can write the method shorter as


 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Ogranos wrote:Btw. if you have such a condition, you can write the method shorter . . .
That is not only shorter, but much more logical. You return the result, rather than messing about with if-elses. There is less risk for error if there is less code. That is why this is the recommended format here.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepakk Verma wrote:if we have a method with return type boolean then is it compulsory to have both true and false returns . . .
To answer the question you asked, rather than the question you thought you asked:

No.

You can have a method which always returns true or false. It is a bit useless, but is legal syntax.The reason you had problems with your first example which didn't compile is that there were circumstances where the method returned nothing at all. If you give "foo" as the return type, the method must return a "foo" from every path.
 
Deepakk Verma
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
allright..that is a lot of valuable information you gave me....
thanks very much to you all especially to Rahul, Campbell and D. Ogranos
Deepak Verma
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic