I extracted someone's APK (Android app) to see the Java source code and saw a lot of return;
code, even on void
methods.
For example:
public void doSomething(){
do{
return; //This line makes the code below unreachable and can't compile in Eclipse
switch(num){
...
default:
return;
}
}while(...)
...
}
How come the app seems to run well on my phone?
I guess return;
is like a shortcut to break from the method. Is that right?
In Java, return is a reserved keyword i.e, we can't use it as an identifier. It is used to exit from a method, with or without a value.
A return is a value that a function returns to the calling script or function when it completes its task. A return value can be any one of the four variable types: handle, integer, object, or string. The type of value your function returns depends largely on the task it performs.
Void (NonValue-Returning) functions: Void functions are created and used just like value-returning functions except they do not return a value after the function executes.
If no return statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined.
return keyword is used basically in a void method ,it helps to break the conditional statement to come out of method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With