java - Don't you have to use the same parameter names when calling a method? -
in code below, i've written 2 methods:
- one has variable
test
defined string - one returns 2 different outcomes depending on typed
i have variable named test
in userinputhere
while parameter in hello
named message
. in userinputhere
use test
instead of message
- why work?
does parameter not matter when invoke hello
method?
i when entering method returns something, has defined , parameters define further method going work on, so, had assumed that, when calling said method method, have use same parameters, doesn't seem case.
import java.util.scanner; public class methodsandparameters { static scanner input = new scanner(system.in); public static void main(string[] args){ userinputhere(); } public static void userinputhere(){ string test = input.nextline(); system.out.println(hello(test)); } public static string hello(string message){ if (message.equalsignorecase("hi")) { return "hello"; } else { return "goodbye"; } } }
when invoke hello
method, parameter (message
) contained inside of hello method itself. allowed pass argument hello
long string (since message
of type string). not matter pass hello
long resolves string. in example, variable test
works fine because string.
the program not invoking "test" or invoking "message", invoking hello
method can take string argument.
Comments
Post a Comment