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?
4 Réponses
+ 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
+ 5
Yes, you can call that function with any nam, for exaple you can write sayHello("avory").
Np man
+ 1
@Filip thanks man. and to clarify further, once we have String name, java would know to go to David and Amy?
0
Magic :), The simple answer is that you call the method twice and the code execution will go on a line by line basis.