System.out.println()与Java返回之间的差异 [英] Differences between System.out.println() and return in Java

查看:126
本文介绍了System.out.println()与Java返回之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解使用 System.out.println()返回blah 在方法中。

I'm trying to understand the difference, and benefits of using System.out.println() vs. return blah in a method.

似乎 System.out.println()用于显示静态信息和 return 是从方法返回的值。然而,我看到的示例如下所示,其中函数在 System.out.println()语句中使用

It seems like System.out.println() is used to display static information, and return is a value returned from the method. Yet I'm seeing examples like the one below, where a function is used within the System.out.println() statement

System.out.println(name.substring(1, 3));

什么时候使用 System.out.println()返回。是否 return 以后可以被另一段代码使用,而 System.out.println()不能?

When is it right to use System.out.println() and return. Is it that return can be used by another piece of code later, whereas System.out.println() cannot?

推荐答案

你的最后一句话实际上是正确的,但这两项操作之间的区别是 HUGE ,所以我想对他们的差异进行更深入的解释。

Your last sentence is effectively correct, but the distinction between these two operations is HUGE, so I'd like to provide a more in depth explanation of their differences.

return 是一条控制程序执行流程的指令。它是Java语法的基础部分。它告诉计算机要执行的代码部分,以及执行期间要使用的值。当您返回一个值时,您说调用此方法的结果是XXXX(XXXX是您返回的值)。

return is an instruction that controls the flow of your program's execution. It is a fundamental part of the Java syntax. It tells the computer what part of your code to execute, and what values to use during that execution. When you return a value, you are saying "The result of calling this method is XXXX" (with 'XXXX' being the value you returned).

System.out.println 不用于控制程序的执行方式。这是告知用户程序内部情况的唯一方法。 System.out.println (简称syso)可以将任何信息打印到控制台;它是变量,表达式还是方法调用的结果并不重要。 静态数据没有限制。

System.out.println is not used to control how your program executes. It is a merely way to inform the user of what is going on inside your program. System.out.println (syso for short) can print any information to the console; it doesn't matter if it's a variable, an expression, or the result of a method call. There is no limitation to "static" data.

让我们看一下它们的实际效果:

Let's look at both of them in action:

int addInts(int arg0, int arg1)
{
    return arg0 + arg1;
}

这意味着我们打电话给 addInts 在我们的程序中,它将评估其参数的总和。因此,当我们写 addInts(3,7)时,它就像写了 3 + 7 10 在我们的源代码中。控制台上没有任何内容;我们所做的只是给我们的程序计算一些东西。

This means that wen we call addInts in our program, it will evaluate to the sum of its arguments. So when we write addInts(3, 7), it's the same as if had simply written 3 + 7 or 10 in our source code. Nothing is printed to the console; all we've done is give our program a way of calculating something.

然而,如果他们只是坐在电脑里面,我们可能做出的任何计算都是无用的,所以我们需要一种方法来向用户显示这些信息。输入syso:

However, any calculations we might make are ultimately useless if all they do is sit inside the computer, so we need a way to display this information to the user. Enter syso:

System.out.println(addInts(22, 16));

调用 addInts 方法并返回38这个值放在计算机内存的某个位置,以便我们的程序可以找到它。

The addInts method is called and returns 38. This value is placed somewhere in the computer's memory such that our program can find it.

接下来,syso获取该值(38)并将其打印到控制台,让用户知道计算了什么值。此程序没有计算任何新内容,我们的程序将继续下一个语句。

Next, syso takes that value (38) and prints it to the console, letting the user know what value was calculated. Nothing new is calculated from this procedure, and our program continues to the next statement.

在简单的程序中,您只有很少的值可以跟踪它是否很容易打印您想要知道的所有内容。例如,如果你正在编写一个程序来做你的代数作业(我去过那里)并且你写了一个方法来解决二次方程式,那么它可能很容易像这样构造它:

In simple programs, you have so few values to keep track of that it can be tempting to just print everything that you want to know where you calculate it. For instance, if you were writing a program to do your algebra homework (I've been there) and you wrote a method to solve the quadratic equation, it might be tempting to structure it like this:

class Algebra
{
    static void quadSolve(double a, double b, double c)
    {
        double result = /* do math...  we're ignoring the negative result here*/;

        System.out.println("The solution to the quadratic equation is: " + result);
    }

    public static void main(String[] args)
    {
        quadSolve(1.0, -6.0, 9.0);
    }
}

然而,如果这种方法很快成为一个非常糟糕的主意你想让你的程序更复杂一点。假设有一个问题需要您解决二次方程,然后使用该计算的结果来计算圆柱体积。在上面的例子中,我们不能这样做:在我们通过syso将 result 的值转储到控制台后,当 quadSolve <时它会消失/ code>方法结束。如果我们有 quadSolve 返回结果并让来电者(地点<$ c)更有意义$ c> quadSolve 被调用处理该值。这是一个更灵活的设计,使我们可以相对轻松地使我们的程序更复杂。这种增加的灵活性和模块化实际上是使方法有用的原因。以下是实现:

However, this approach quickly becomes a very bad idea if you want to make your program a little more complex. Let's say one problem requires you to solve the quadratic equation and then use the result of that calculation to calculate the volume of a cylinder. In the above example, we can't do that: after we dump the value of result to the console via syso, it disappears when the quadSolve method ends. It would make much more sense if we have quadSolve return result and let the "caller" (the place quadSolve was called from) deal with handling that value. This is a much more flexible design that allows us to make our programs much more complicated with relative ease. This increased flexibility and modularity is really what makes methods useful. Here is the implementation:

class Algebra
{
    static double quadSolve(double a, double b, double c)
    {
        double result = /* do math...  we're ignoring the negative result here*/;

        return result;
    }

    public static void main(String[] args)
    {
        double x = quadSolve(1.0, -6.0, 9.0);
        //now we can do whatever we want with result: 
        //print it, negate it, pass it to another method-- whatever.
        System.out.println("The solution to the quadratic equation is: " + x);
        System.out.println("And it's square is: " + (x * x));
    }
}

我希望这可以解决问题。如果您需要进一步澄清,请随时询问。

I hope this clears things up. Feel free to ask if you need additional clarification.

这篇关于System.out.println()与Java返回之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆