Comp 110 Parameters vs. Arguments


A parameter is a placeholder for a value used in a method signature in order to allow for coding without knowing the actual value that is going to be supplied. I know that definition can seem a little confusing, but take a look at the examples below.

//Example 1: A method to add two numbers and return the sum
public int sumMethod(){
    return 2+2;

So, in Example 1, we returned the sum of 2 + 2, which is 4. But what if we wanted to be able to add any two numbers together? This is where Parameters come in. Parameters are specified within the parenthesis of a constructor and method declarations. They look like variable declarations, because they are! Take a look at a method that supplies parameters

//Example 2: A method to add two numbers and return the sum
public int sumMethod (int x, int y)
    return x + y;

So the "int x" and "int y" are the parameters for the method. This means that when the method is called, you must provide the method with two integer arguments.

Ultimately, parameters allow constructors and methods to require additional pieces of information in order to be called. 

What effect does declaring parameters have?

When a constructor or a method declares parameters, it is saying "you need to give me more information in order to call me". 

In the following example, the class' constructor is saying "in order to make a new Rectangle object, you must provide two double values (presumably the length of the two sides).

public class Rectangle {

  // Constructor Declaration
  public Rectangle(double x, double y) {
     // ...


When it is used later, you can see this Rectangle object being constructed with the two double values given to the constructor in parenthesis. 

public class RectangleTest {

  public static void main(String[] a) {
     Rectangle a = new Rectangle(1.0, 2.0);


Arguments are what satisfy the parameter of a method, and they must be the same type as the parameters given. Taking the example 1 sumMethod from above, this is how you would call the method. In the rectangle constructor usage example, when Rectangle a = new Rectangle(1.0,2.0) is called, 1.0 and 2.0 are an example of arguments

//Example 1 Method
sumMethod(); //returns 4

So with no parameters, there is no placeholder for us to put arguments. This means that no matter how many different times you call the sumMethod() method, it will always return 4.

However, if we are using the second example, there are many different things we can do with it

//Example 2 Method
sumMethod(2, 3); //returns 5
sumMethod(3, 4); //returns 7
sumMethod(9, 6); //returns 15
sumMethod(1000, 4000); //returns 5000
sumMethod("4", 3); //Doesn't work! the "4" is of type String, not int

Since the second sum method requires two integer arguments, you are able to supply any two integers and it will return a sum of the two back to you.

Using arguments and parameters can get a lot more complicated, and allow you to do much more with your methods when you can change the values based on the situation, without modifying the actual code. 

Arguments are expressions and therefore have a type. The type and number of arguments must correspond one-to-one with the type and number of parameters. Order matters!

Just remember, parameters are the placeholders, and arguments are the actual values.