Differences between System.out.println () and Java Returns

I am trying to understand the difference and advantages of using System.out.println() vs. return blah in the method.

It seems that System.out.println() used to display static information, and return is the value returned from the method. However, I see examples like the ones below where the function is used in the expression System.out.println()

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

When to use System.out.println() and return correctly. Is it possible that return can be used by another part of the code later, while System.out.println() cannot?

+6
source share
3 answers

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 = /* 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 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 = /* 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 square is: " + (x * x)); } } 

I hope this clarifies the situation. Feel free to ask if you need further clarification.

+18
source

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

Information can be "printed" into the output stream of System.out.println() .

Both have their own uses ... which are usually orthogonal.

+5
source

They have very little in common with each other.


System.out.println() used to output lines to the console / terminal. So

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

should print the string "Hello world"


return is a Java instruction to return to the code that called the method and pass the 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 } 
+1
source

Source: https://habr.com/ru/post/974237/


All Articles