Can anybody provide me with crystal clear difference between method's parameter and argument. How both of them work ?
Under discussion
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
30 October 2020, 16:17
They are used interchangeably, and no one except the most pedantic of people will chastise you for using them to refer to the other. But, here are the correct usages of each:
Take this bit of code for example:
In this bit of code you have 3 methods (main, metho1, method2). Each method declares its parameters inside the parentheses where they are declared (lines 1, 6, and 9). Matching these parameters is absolutely required to correctly call the method.
Now on lines 2 and 3 is code that calls the m1 and m2 methods. To correctly call a method you must use the method name and pass arguments inside parentheses that match the method parameters. So the parameters of m1 are (int, String) and on line 2 i pass in (4, "test") (and int and a String) which match the method parameters. This code works, however if I changed that line to:
The code would fail because the arguments passed into the method of an (int) do not match the parameters of (int, String). On line 3, where I call m2 I pass the arguments ('A', 5.6) which are a char and a double, and this code works because those arguments match the parameters of the method which are (char, double).
So:
Parameter(s) - these are inside the parentheses of a method declaration
Arguments(s) - these are inside the parentheses when a method is called and MUST match the method's parameters. +1
Dinesh
30 October 2020, 17:24
Thanks a lot. Now, I got it
I have one question. Please make me understand that too.
1. public static void mian(string []args) is a method declaration at line 1 of the code
2. execution of program starts from main() in java
3. what arguments gets passed in main() method when execution starts?
I think in that case
the argument that get passed in main() is the name of file with .java extension
suppose i have my file abc.java than abc will be passed in main()
thorough the command
java abc
Am I right ?
0
Guadalupe Gagnon
30 October 2020, 17:51
The main method uses command line arguments (from outside the java code) which are passed in when starting the program from the command line. IntelliJ and pretty much all IDE's do this for you behind the scenes, though the top of the output window when you run code shows the command line arguments that start your code. For example, when I just run a test class that I have this is printed to the top of the output window before any of my code is run:
"C:\Program Files\Java\jdk-12.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=65114:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Programming\CodeGymTasks\October 2019\CodeGymTasks\out\production\1.JavaSyntax" Test.Solution
The main method uses a special type of argument called varArgs (short for variable arguments) meaning that they can contain any amount including none. You will learn how to make methods with varArgs later on.
0
Dinesh
30 October 2020, 18:55
Fine. Thank You
0