Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does return; (without value) mean?

Tags:

java

return

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?

like image 548
hrsetyono Avatar asked Mar 04 '13 03:03

hrsetyono


People also ask

What does return without value do in Java?

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.

What does returning a value mean?

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.

Is a return type that means no value is returned?

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.

What happens if you don't return value from function?

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.


1 Answers

return keyword is used basically in a void method ,it helps to break the conditional statement to come out of method.

like image 50
Vishnu Dubey Avatar answered Nov 15 '22 15:11

Vishnu Dubey