Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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.