Search code examples
javareturn

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?


Solution

  • 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.