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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:15:23  来源:igfitidea点击:

What does it mean to return a value?

javareturn-valuereturn

提问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 getXwould return the value of xto 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 10and returning it to method1. If method2were returning an Objectthen 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 method1copies 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 valuein method1is unaffected by method2(valuestill equals 5 after method2executes), because only a copy of valuewas sent as a parameter.

valuemethod1是未受影响method2value后仍等于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.

您只是调用一个返回整数的方法,但您从未使用/打印它。尝试在您的代码中使用它,看看您是否获得了您在类中设置的所需值。