Java Java Objects Meet Objects Final

lowell seitz
lowell seitz
631 Points

Why does java "return" statement variable name that it will be returning not have to match the expected variable name i

Why does java "return" statement variable name not have to be the same expected variable name in the method name. What happens when you return the orginalCharacterName, but the method is expecting CharacterName as the return argument/variable. public String swapHead(String characterName) { String originalCharacterName = this.characterName; this.characterName = charaterName; return originalCharacterName; }

2 Answers

Fahad Mutair
Fahad Mutair
10,356 Points

hi lowell seitz ,

public String swapHead(String characterName)

(String characterName) this is the parameter not the return and when you call method with argument it will be set to this parameter

public String swapHead < the string word here means it should return String variable that's why we typed this line return originalCharacterName; and it already declared as String at this line String originalCharacterName

lowell seitz
lowell seitz
631 Points

Thank you I understand now