0

JAVA Method Parameters

I'm referring to the code below from lesson here: class MyClass { static void sayHello(String name) { System.out.println("Hello " + name); } public static void main(String[ ] args) { sayHello("David"); sayHello("Amy"); } } // Hello David // Hello Amy How does JAVA know to print both "Hello David" & "Hello Amy", since there's only one println?

3rd Jan 2017, 1:19 AM
avory
4 odpowiedzi
+ 7
Because you are calling the function 'sayHello' twice in main method, you have sayHello("David") which gets the "Hello David" printed, and then you have sayHello("Amy) which then prints "Hello Amy". Hope you understand
3rd Jan 2017, 2:04 AM
Filip
Filip - avatar
+ 5
Yes, you can call that function with any nam, for exaple you can write sayHello("avory"). Np man
3rd Jan 2017, 7:17 AM
Filip
Filip - avatar
+ 1
@Filip thanks man. and to clarify further, once we have String name, java would know to go to David and Amy?
3rd Jan 2017, 4:42 AM
avory
0
Magic :), The simple answer is that you call the method twice and the code execution will go on a line by line basis.
3rd Jan 2017, 2:21 AM
joseph odibo
joseph odibo - avatar