What is the difference between return and print?


#1



https://www.codecademy.com/en/courses/learn-java/lessons/object-oriented-programming/exercises/using-methods-2?action=lesson_resume


I'm just wondering why should I use the "return" while I make method.
What's the difference between return and print?

For example,

int spikeAge = spike.getAge();
System.out.println(spikeAge);

is the correct code.

(Of course there is

public int getAge() {
return age;}

before those codes)

but why can't I just write

System.out.println(age)?

the consequence that I get would be the same, isn't it?


Replace this line with your code.


#2

Printing is a one-off action that has no outcome or artifact. We print, it's done. End of story. Printing is also completely removed from the program's flow and its data.

Return has a continuation implied, but there is more. Just as a function takes an argument from a remote scope, the return value is sent back to the caller scope. It's an open channel to any scope from which it is called, and practically any object or expression can be passed back through it.