• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

understanding return types ..

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic