hrsetyono
hrsetyono

Reputation: 4464

What does return; (without value) mean?

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?

Upvotes: 12

Views: 31121

Answers (4)

Denis Tulskiy
Denis Tulskiy

Reputation: 19187

To answer your first question: I assume this code is decompiled. Note that decompilers are not one to one converters of binary dex code to whatever Java code has been used to generate these binaries. They often have problems with parsing different control structures like loops and switches. Besides, the binary may be obfuscated, meaning that after compilation the binary is modified in a way to be fully operational, but harder to decompile and reverse engineer (basically to prevent what you're trying to do here :) ). They can add dead code, like return statement that shouldn't be there, mangle loops and if statements to confuse decompiled code, etc.

Upvotes: 3

Vishnu Dubey
Vishnu Dubey

Reputation: 151

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

Upvotes: 1

Brad
Brad

Reputation: 9223

If the method returns void then return; just exits out of the method at that statement, not running the following statements.

Upvotes: 26

SirPentor
SirPentor

Reputation: 2044

Yes, that is correct. Look at the bottom of http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html:

The return Statement

The last of the branching statements is the return statement. The return statement exits from the current method, and control flow returns to where the method was invoked. The return statement has two forms: one that returns a value, and one that doesn't. To return a value, simply put the value (or an expression that calculates the value) after the return keyword.

return ++count; The data type of the returned value must match the type of the method's declared return value. When a method is declared void, use the form of return that doesn't return a value.

return;

Upvotes: 11

Related Questions