Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Objects Meet Objects Methods

João Ignácio Brito
João Ignácio Brito
1,201 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

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,201 Points

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