Your last sentence is effectively correct, but the difference between these two HUGE operations, so I would like to give a more detailed explanation of their differences.
Difference:
return is an instruction that controls the flow of your program. This is a fundamental part of Java syntax. It tells the computer which part of your code to execute, and which values ββto use during this execution. When you return the value, you say: "The result of calling this method is XXXX" (with "XXXX" is the value you are returning).
System.out.println not used to manage your program. This is an easy way to inform the user about what is happening inside your program. System.out.println (syso for short) can print any information on the console; it doesnβt matter if it is a variable, expression, or the result of a method call. There are no restrictions on βstaticβ data.
Look at both of them in action:
int addInts(int arg0, int arg1) { return arg0 + arg1; }
This means that in our program we will call addInts , it will calculate the sum of its arguments. Therefore, when we write addInts(3, 7) , it is just as if it just wrote 3 + 7 or 10 in our source code. Nothing is printed to the console; all we did was give our program a way to figure out something.
However, any calculations that we could do are ultimately useless if everything they do is 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 addInts and returns 38. This value is placed somewhere in the computer's memory so that our program can find it.
Syso then takes this 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 proceeds to the next statement.
So what am I using?
In simple programs, you have so few values ββto track that it may be tempting to simply print everything you want to know where you calculate it. For example, if you write a program to do your homework in algebra (I was there), and you wrote a method for solving the quadratic equation, you may be tempted to structure it as follows:
class Algebra { static void quadSolve(double a, double b, double c) { double result = ; 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 of the tasks requires you to solve the quadratic equation, and then use the result of this calculation to calculate the volume of the cylinder. In the above example, we cannot do this: after we reset the result value to the console via syso, it disappears when the quadSolve method quadSolve . This makes sense if we have a quadSolve return result , and let the caller (from which the quadSolve location was quadSolve ) handle this value. This is a much more flexible design that allows us to make our programs much more complex with relative ease. This increases flexibility and modularity, which makes methods useful. Here is the implementation:
class Algebra { static double quadSolve(double a, double b, double c) { double result = ; return result; } public static void main(String[] args) { double x = quadSolve(1.0, -6.0, 9.0);
I hope this clarifies the situation. Feel free to ask if you need further clarification.