File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Return type concept problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Return type concept problem" Watch "Return type concept problem" New topic
Author

Return type concept problem

Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
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

Joined: Jul 26, 2007
Posts: 9990
    
    7

Did your code compile?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1613
    
  10

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

When you do things right, people won't be sure you've done anything at all.
Deepakk Verma
Ranch Hand

Joined: Sep 09, 2009
Posts: 31
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

Joined: Sep 26, 2009
Posts: 188
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

Joined: Sep 09, 2009
Posts: 31
ok....now i got it
Thanks Mr.Rahul
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
welcome
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
Btw. if you have such a condition, you can write the method shorter as


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Oct 13, 2005
Posts: 36508
    
  16
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

Joined: Sep 09, 2009
Posts: 31
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Return type concept problem
 
Similar Threads
what is the difference between == and equals
Class Already Defined
Using Objects as Parameters
Difference between Object's equals() and String's equals() method ?
equals doubt??