Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

the return keyword

 
Emanuel Mensa
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64608
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the return type of the method.

void means nothing.
 
Ishan Pandya
Ranch Hand
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all's clear now
 
Jinu Vijay
Greenhorn
Posts: 20
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 226
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic