Java 返回值是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16312323/
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 it mean to return a value?
提问by user2338180
I'm fairly new to programming, and I'm confused about what it means exactly to return a value. At first, I thought it meant to output what value is being returned, but when I tried that in my own code, nothing happened.
我对编程还很陌生,我对返回值的确切含义感到困惑。起初,我认为这意味着输出返回的值,但是当我在自己的代码中尝试时,没有任何反应。
class Class1 {
public static int x = 3;
public static int getX(){
return x;
}
public static void main(String[] args){
Class1.getX();
}
}
This is an example of what I mean. When I run the program, nothing shows up. Considering this, I'm led to believe returning a value means something else. But what?
这是我的意思的一个例子。当我运行程序时,没有任何显示。考虑到这一点,我相信返回一个值意味着别的东西。但是什么?
回答by MadProgrammer
In simple terms, it means to return the value to caller of the method...
简单来说,就是将值返回给方法的调用者......
So, in your example, the method getX
would return the value of x
to the caller, allowing them access to it.
因此,在您的示例中,该方法getX
会将 的值返回x
给调用者,允许他们访问它。
class Class1{
static int x = 3;
public static int getX(){
return x;
}
public static void main(String args[]){
int myX = Class1.getX(); // return the value to the caller...
System.out.println(myX); // print the result to the console...
}
}
回答by Zim-Zam O'Pootertoot
Returning a value is a way for methods to talk to each other
返回值是方法之间相互通信的一种方式
public void method1() {
int value = 5 + method2(5);
System.out.println(value);
}
public int method2(int param) {
return param + 5;
}
This will print 15 (5 gets sent to method2
, which adds 5 to it and returns the result to method1
, which adds 5 to it and prints the result).
这将打印 15(5 被发送到method2
,将 5 添加到它并将结果返回到method1
,将其添加到 5 并打印结果)。
Java returns copies of values - in this case, it's copying the value 10
and returning it to method1
. If method2
were returning an Object
then it would return a copy of the object's reference. Different languages have different semantics for method returns, so be cautious when switching between languages. Java also copies the values of parameters passed to methods - in this case method1
copies the value 5 and passes it to method2
.
Java 返回值的副本 - 在这种情况下,它复制值10
并将其返回到method1
. 如果method2
返回 anObject
那么它将返回对象引用的副本。不同语言对方法返回有不同的语义,所以在语言之间切换时要小心。Java 还复制传递给方法的参数值——在这种情况下,method1
复制值 5 并将其传递给method2
.
public void method1() {
int value = 5;
method2(value);
}
public void method2(int param) {
param = param + 5;
}
The value
in method1
is unaffected by method2
(value
still equals 5 after method2
executes), because only a copy of value
was sent as a parameter.
在value
在method1
是未受影响method2
(value
后仍等于5method2
执行),因为只有一个拷贝value
被作为参数发送。
回答by Juned Ahsan
You are just calling a method which returns an integer but you are never using/printing it. Try to use it in your code to see whether you have got the desired value as you have set in your class.
您只是调用一个返回整数的方法,但您从未使用/打印它。尝试在您的代码中使用它,看看您是否获得了您在类中设置的所需值。