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 (Retired) Creating the MVP Prompting for Guesses

Can you pass a returned value as a parameter for another method as long as it has the correct type?

In following scenario, why passing a value named "guess" from promptForGuess() works with applyGuess(Char letter)? when the latter one is specifically asking a parameter Char named "letter"? Is that just a placeholder?

Can I use applyGuess with any names so long as the passing value is workable with Char?

//from Prompter.java
  public boolean promptForGuess() {
    Console console = System.console();
    String guessAsString = console.readLine("Enter a letter:  ");
    char guess = guessAsString.charAt(0);
    return mGame.applyGuess(guess);

//from Game.java
  public boolean applyGuess(Char letter) {
    boolean isHit = mAnswer.indexOf(letter) >= 0;
    if (isHit) {
      mHits += letter;
    } else {
      mMisses += letter;
    }
    return isHit; 
  }

2 Answers

You are exactly right. The name that you give when defining the parameter is simply a placeholder for you to use when creating the function. You can pass a variable with any name into the function, so long as it conforms to the type.

Thanks for the quick clarification!

Rob Bridges
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,459 Points

Hey there YoungChul.

You are correct, applyGuess takes a character as parameter, as long as you pass it in a character to check, it will do it's task, you'd of course run into trouble if you tried to pass it an int or a boolean, but what's above is perfectly acceptable Java.