I extracted someone'southward APK (Android app) to encounter the Java source lawmaking and saw a lot of return; code, even on void methods.

For example:

                public void doSomething(){ do{     return; //This line makes the code beneath unreachable and can't compile in Eclipse     switch(num){         ...         default:             render;     } }while(...) ... }                              

How come up the app seems to run well on my telephone?

I guess return; is like a shortcut to break from the method. Is that correct?

Pang

8,971 145 gold badges 82 silver badges 117 statuary badges

asked Mar 4, 2013 at three:45

5

  • Decompilers aren't ever perfect.

    Mar 4, 2013 at 3:46

  • Every method eventually returns. So a method with a void return type can either render; or leave it out. It is not needed and is a matter of preference.

    Mar iv, 2013 at 3:46

  • @Vulcan so they are corrupted function of the lawmaking?

    Mar 4, 2013 at 3:49

  • @squiguy it is more a matter of preference. It serves a useful purpose that exceptions can't - it lets you end an execution path without breaking it. what I mean is that you tin can return and go along on the side by side line outside of that telephone call versus throwing an exception and inbound a completely different execution path in the first grab argument. so while its not needed in void methods all the time, it is still more than personal preference.

    Jul vi, 2022 at 19:43

four Answers 4

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

answered Mar 4, 2013 at 3:52

1

  • @HussainAkhtarWahid yeah I think it returns nothing

    Mar 4, 2013 at 4:40

Yes, that is correct. Look at the lesser of http://docs.oracle.com/javase/tutorial/coffee/nutsandbolts/co-operative.html:

The render Argument

The final of the branching statements is the return argument. The return statement exits from the current method, and control catamenia returns to where the method was invoked. The return statement has 2 forms: one that returns a value, and ane that doesn't. To render a value, merely put the value (or an expression that calculates the value) after the return keyword.

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

return;

answered Mar 4, 2013 at 3:48

ane

  • Thanks! I never knew that void method tin use return.

    Mar 4, 2013 at 3:52

To answer your beginning question: I assume this code is decompiled. Note that decompilers are non one to one converters of binary dex code to whatsoever 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 fashion to be fully operational, but harder to decompile and contrary 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.

answered Mar 4, 2013 at 4:01

one

  • Thanks it does explains why all the for and while loops seem to have trouble.

    Mar 4, 2013 at iv:xiii

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

answered Apr 10, 2022 at 17:04

Not the answer you're looking for? Browse other questions tagged java return or ask your own question.