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

what exactly is the purpose of the return keword in a void method ?

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think it is optional to use return //(without expression)
in a method that has a void return type.
what does this return; //(without expression)
accomplish, does it behave exactly like break ?
according to my understanding it immediately escapes the current method and tranfers control to the caller of the method, am i right ?
what if break were used instead, wouldnt it accomplish exactly the same result, please clarify !!
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your understanding is right. The return statement at the end of the void method is redundant, though nice to have from a desciplined programming perspective. However the return statement in places other than the last line of the method helps stop the execution of following code and return the control to the calling method. This can be very helpful if you want to stop execution and return from a deeply nested control block. For example,

The break statement may not serve as an alternative to the return statement since it ( former ) can be used only in loop-structures. In the example above, since the control is in a if-block, we cannot use the break statement.
Hope that helps,
Ajith

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ajith Kallambella:

The break statement may not serve as an alternative to the return statement since it ( former ) can be used only in loop-structures.


Ajith
but i think break can be used any where there is a possibility of creating a local scope. whether it may be a if-else or swith or try-catch etc construct.
here is supporting code.

<pre>
class Test {
public static void main(String arg[]) {
int i=8;
label1:
if (i>3)
{
System.out.println("label allowed");
break label1;
}
label2:
switch(i)
{
case 1: System.out.println("again allowed");
break label2;
}

label3:
try {
System.out.println("again allowed");
break label3;
}
catch(RuntimeException re) {}
}
}
</pre>
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to Mughal break can be used in" labeled blocks, loops( for, while , do-while), switch statements".
in continuation of original question : some expressions and statements resulting the return of a program flow,as result sometimes an exspression never reached by a program flow. Whether exist a scope of those conditions or norms?
 
Today you are you, that is turer than true. There is no one alive who is youer than you! - Seuss. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic