java 返回什么?(没有价值)是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15194243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does return; (without value) mean?
提问by hrsetyono
I extracted someone's APK (Android app) to see the Java source code and saw a lot of return;
code, even on void
methods.
我提取了某人的 APK(Android 应用程序)以查看 Java 源代码并看到了很多return;
代码,甚至在void
方法上。
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?
我想return;
这就像打破方法的捷径。是对的吗?
采纳答案by Denis Tulskiy
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.
回答你的第一个问题:我假设这段代码是反编译的。请注意,反编译器不是二进制 dex 代码到用于生成这些二进制文件的任何 Java 代码的一对一转换器。他们在解析不同的控制结构(如循环和开关)时经常遇到问题。此外,二进制文件可能会被混淆,这意味着在编译后二进制文件被修改为完全可操作的方式,但更难反编译和逆向工程(基本上是为了防止你在这里尝试做的事情:))。他们可以添加死代码,例如不应该存在的 return 语句、mangle 循环和 if 语句以混淆反编译代码等。
回答by Brad
If the method returns void then return;
just exits out of the method at that statement, not running the following statements.
如果该方法返回 void,则return;
仅在该语句处退出该方法,而不运行以下语句。
回答by SirPentor
Yes, that is correct. Look at the bottom of http://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html:
对,那是正确的。查看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;
退货声明
最后一个分支语句是 return 语句。return 语句从当前方法退出,控制流返回到调用方法的地方。return 语句有两种形式:一种返回值,一种不返回值。要返回一个值,只需将值(或计算该值的表达式)放在 return 关键字之后。
返回++计数;返回值的数据类型必须与方法声明的返回值的类型相匹配。当一个方法被声明为 void 时,使用不返回值的返回形式。
返回;
回答by Vishnu Dubey
return keyword is used basically in a void method ,it helps to break the conditional statement to come out of method.
return 关键字基本上用在 void 方法中,它有助于打破条件语句从方法中出来。