Differences between System.out.println() and return in Java
Asked Answered
D

4

12

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

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));

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?

Doukhobor answered 22/8, 2014 at 22:32 Comment(6)
You're comparing potatoes with solar beams, and you're getting purple unicorns flying around stonehenge waiting for a meteor bringing back dinosaurs.Drawers
I'd return blah if feasible. It means you can do what you want with the value. printlin() is fixed and you might not always want to print.Scarabaeus
I'd like to discourage users from downvoting this question. It may seem stupid, but I've taught java beginners before and for this level of knowledge this is actual a well-put forth question.Nutlet
@ValekHalfHeart While I didn't downvote it and agree with you in it being a well written question, the tooltip for downvoting says "This question does not show any research effort, it is unclear or not useful.". I think it kind of falls a bit in "does not show any research effort", because googling for "java syso" and "java return" would answer this question pretty quickly.Schreib
@1337 If you haven't already, please read my answer. The issue lies not in understanding what these things do, but when to use them. The rules we all take for granted are not as evident in trivial programs.Nutlet
@ValekHalfHeart I read it (and it is a really good answer btw. +1 from me). But you are right, things seem more and more trivial over time and we should respect that it might not be trivial at all.Schreib
N
30

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.

The Difference:

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 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;
}

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));

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.

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.

So which do I use?

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);
    }
}

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.

Nutlet answered 22/8, 2014 at 22:41 Comment(2)
"has no effect whatsoever on how your program executes, unless an error occurs", well, one could put a method call and assignments in it which could change how the program executes ;) This is an amazing answer anyway, +1Schreib
Thank you for the thorough explanation, definitely clears things up and explains why return allows code to be more versatile.Doukhobor
A
6

A method often returns a value (which is done by using the return statement).

Information may be "printed" to an output stream by System.out.println() references.

They both have their uses ... which are usually orthogonal.

Avarice answered 22/8, 2014 at 22:39 Comment(0)
A
2

They have very little to do with each other.


System.out.println() is used to output strings to a console/terminal. So

System.out.println("Hello world");

should output the string "Hello world"


return is a statement in Java to go back to the code that invoked the method and pass back a value.

public static int add(int a, int b) {
    return a + b;  // go back to method that called this method (main)
}

public static void main(String[] args) {
    int sum = add(3,4);  // the return result (7) is stored in sum
}
Animus answered 22/8, 2014 at 22:37 Comment(1)
But you omit the vs explanation =\Drawers
E
0

From my understanding, and to give a simple answer which has a significant meaning, I would say:

It is like when you go to a coffee shop and order a cup of coffee, but the barista tells you if you want a picture of the coffee cup or the cup itself :).

System.out.println() is the picture, while.. return blah is the cup of coffee.

With "return" you can do what you like with the value of return, but with "print" you only see what the function is doing.

Exmoor answered 23/8, 2021 at 12:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.