This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes the return keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "the return keyword" Watch "the return keyword" New topic
Author

the return keyword

Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
usually i use the return like this



but i'm reading something now and i just saw this:



if (boolean)
return //now return what?

so what is that returning? i don't really get this part...

any help would be appreciated
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Return says, "end this method right now, without executing any more of its code, and return control to the calling method (except first execute the try finally blocks of any enclosing try or catch blocks I'm currently in)."

It always does that.

If you put a value with it, it also says, "...and make this value the result of calling this method."
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Look at the return type of the method.

void means nothing.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219



Try to compile the program after replacing void return type with int or any other datatype.


OCPJP
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
Jeff Verdegan wrote:except first execute the try blocks of any enclosing try or catch blocks I'm currently in

except first execute the finally blocks...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Joanne Neal wrote:
Jeff Verdegan wrote:except first execute the try blocks of any enclosing try or catch blocks I'm currently in

except first execute the finally blocks...


GAAAH!!! Can't believe I did that. Thanks for the correction.
Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
all's clear now
Jinu Vijay
Greenhorn

Joined: Sep 19, 2011
Posts: 17

Ishan Pandya does highlight a valid point. The statement



would compile only if the method has a void return type. If not, you have to specify a return parameter with the matching type of the method.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jinu Vijay wrote:Ishan Pandya does highlight a valid point. The statement



would compile only if the method has a void return type. If not, you have to specify a return parameter with the matching type of the method.


Right. What I said is true, but it doesn't imply that we can arbitrarily choose "return" or "return X" interchangeably any old where.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 219

Jinu Vijay wrote:
would compile only if the method has a void return type. If not, you have to specify a return parameter with the matching type of the method.


I didn't mean that jinu. It was like keeping a return type of a method and only returning a value from the "if" condition will make the compiler sad..
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ishan Pandya wrote:
Jinu Vijay wrote:
would compile only if the method has a void return type. If not, you have to specify a return parameter with the matching type of the method.


I didn't mean that jinu. It was like keeping a return type of a method and only returning a value from the "if" condition will make the compiler sad..


Both points boil down to the same thing: Every normal completion of a non-void method must return a value of the appropriate type, else it's a compile-time error.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: the return keyword
 
Similar Threads
Overloaded constructors gives Duplicate method in type error
The level of a Node in a Binary Tree
Putting Binary Search Tree into an array
void type not allowed
cannot find symbol class Dog