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 (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,467 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.