Java Java Objects Meet Objects Methods

João Ignácio Brito
João Ignácio Brito
1,200 Points

Return/Printf

Wouldn't the printf do the same as the return method? In this case... If no, why not? Thank you.

2 Answers

Steve Hunter
MOD
Steve Hunter
Treehouse Moderator 57,656 Points

Hi there,

No. The printf method outputs something to the standard output (the screen). The return keyword returns the parameter type/value back to the place in the code where the method was called from.

I'll try to make an example:

printf("Steve"); // outputs "Steve" to the screen

public String returnName(){
  return "Joao";
}

printf(returnName()); // example 2

Here, the first line outputs my first name to the screen. In the next example, I call the badly named method, returnName, which does the same thing with your name by using printf too. BUT the method, returnName returned the value of your name which the printf method then used.

So, return passes a piece of data back from where the method was called from. The printf method just prints stuff to the screen.

Make sense?

Steve.

João Ignácio Brito
João Ignácio Brito
1,200 Points

Ohhh thanks a lot man, now it makes sense to me.