• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Return type concept problem

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did your code compile?
 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok....now i got it
Thanks Mr.Rahul
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Btw. if you have such a condition, you can write the method shorter as


 
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80278
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic