Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between arguments and parameters in Java [duplicate]

Possible Duplicate:
What's the difference between an argument and a parameter?

I was going through some interview questions. I wasn't able to come up with a solid answer to this question:

Difference between arguments and parameters in Java?
How are they different?

like image 311
AppSensei Avatar asked Oct 03 '12 12:10

AppSensei


People also ask

What is the difference between argument and parameters?

The values that are declared within a function when the function is called are known as an argument. The variables that are defined when the function is declared are known as parameters. These are used in function call statements to send value from the calling function to the receiving function.

What is an argument in Java?

A java argument is a value passed to a function when the function is called. Whenever any function is called during the execution of the program there are some values passed with the function.

Are arguments and actual parameters same?

When a function is called, the values (expressions) that are passed in the function call are called the arguments or actual parameters. The parameter used in function definition statement which contain data type on its time of declaration is called formal parameter.


2 Answers

Generally a parameter is what appears in the definition of the method. An argument is the instance passed to the method during runtime.

You can see a description here: http://en.wikipedia.org/wiki/Parameter_(computer_programming)#Parameters_and_arguments

like image 198
yiannis Avatar answered Oct 04 '22 10:10

yiannis


The term parameter refers to any declaration within the parentheses following the method/function name in a method/function declaration or definition; the term argument refers to any expression within the parentheses of a method/function call. i.e.

  1. parameter used in function/method definition.
  2. arguments used in function/method call.

Please have a look at the below example for better understanding:

package com.stackoverflow.works;  public class ArithmeticOperations {      public static int add(int x, int y) { //x, y are parameters here         return x + y;     }      public static void main(String[] args) {         int x = 10;         int y = 20;         int sum = add(x, y); //x, y are arguments here         System.out.println("SUM IS: " +sum);     }  } 

Thank you!

like image 41
1218985 Avatar answered Oct 04 '22 08:10

1218985