Java 中 System.out.println() 和 return 的区别 [英] Differences between System.out.println() and return in Java

查看:46
本文介绍了Java 中 System.out.println() 和 return 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解在方法中使用 System.out.println()return 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 合适.是不是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?

推荐答案

你的最后一句实际上是正确的,但是这两个操作之间的区别是巨大,所以我想提供更多深入解释它们的差异.

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 + 710 一样.控制台没有打印任何内容;我们所做的只是为我们的程序提供了一种计算方式.

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.

然而,如果我们所做的任何计算都位于计算机内部,那么我们可能进行的任何计算最终都是无用的,因此我们需要一种方法来向用户显示这些信息.输入系统:

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方法结束时消失.如果我们让 quadSolve 返回 result 并让调用者"(调用 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.

这篇关于Java 中 System.out.println() 和 return 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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