File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

understanding return types ..

 
Nick Faiz
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is an oddity I don't quite understand.
Can anyone shed some light on why foo(), here, which has no return type whatsoever (i.e. void) operates quite well with an empty return statement?
If it seems odd, I am using software that specifies methods be written as foo() is.

public class TestingReturnType {

public final static void foo()
{
System.out.println( 1 ) ;
return ;
}

public static void main( String[] args )
{
foo() ;
}
}
Thanks,
biv
[ January 29, 2003: Message edited by: brain_in_vat ]
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy!
Good question. The 'return' keyword means 'return from this method NOW (popping the method off the stack and returning to the calling method)'. If there *is* a non-void return type, the return MUST be followed by a value or variable that can be legally assigned to the return type. (return 2; return new Dog(); etc.)
So in the code that you have, you're right -- the return statement has no purpose since that's the end of the method anyway. But... you *could* do something like:
void myMethod() {
if (x > y) {
doStuff();
return; // says get out NOW!
}
// continue on
// more code
}
In this context (with a void return type), the 'return' is kind of like a 'break from the whole method' statement.
Of course, this isn't necessarily a good coding practice, but that's a different story...
Bottom line: you can always have a return statement in a method. In a method with a non-void return type, the return MUST be followed with a value/variable that is compatible with the return type of the method.
In either case, return ALWAYS means "get out of the method now and return control to the calling method'".
Except... could there be a case where a return will NOT immediately take you out of the method�where the current method stays on the stack for just a little while longer before the actual 'return' happens?
I'll leave that as an exercise for those of you just starting your studies
So that's the question:
Under what circumstances, if any, does a 'return' statement not cause the current method to pop off the stack immediately?
cheers,
Kathy
 
Jasper Vader
Ranch Hand
Posts: 284
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to have a go at your question Kathy: when a return statement indicates something like
return new Dog();
and the constructor for Dog has a couplke of method calls within it that holds things up for a while?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
brain_in_vat,
Welcome to Javaranch, a friendly place for Java greenhorns
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Nick Faiz
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kathy,
Thanks for your answer!
To hazard a guess, for an answer to your question -
// pseudocode
try
{
...
return ;
...
}
catch (SomeException e)
{
return ;
...
}
finally
{
System.out.println( "!" ) ;
}
I'm a little intrigued - where can I learn about the stack that contains currently executing methods - the JLS?
I'm actually a SCJP and am beginning study for the SCWCD quite soon. It's great to learn more about the fundamentals, however.
[ January 30, 2003: Message edited by: Nick Faiz ]
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a chapter of Inside The Java 2 Virtual Machine and from Richard G. Baldwin
Processing stack trace in Java
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic