Java

What are Parameters in Java

Java provides a concept of parameters that is associated with the user-defined methods and constructors. In java, the user-defined methods and constructors can take zero, one or more than one parameter. Here the query is what are the parameters in java? Well! The parameters in java are variables that can be passed to the methods or constructors and can be of any data type such as byte, string, int, float, etc. In java, the methods/constructors can take a single or multiple parameters.

This article will provide an in depth understanding of the below-listed concepts:

  • What are parameters in java
  • Parameters vs Arguments in Java
  • Examples
  • Conclusion

So, let’s get started!

What are parameters in java

The parameters in java are variables that can be passed to the methods or constructors. Within these methods/constructors these parameters behave as variables.

Parameters vs Arguments in Java

Let’s consider a simple java program to understand the difference between the parameters and arguments.

How to show a user-defined method that accepts a parameter

The below snippet shows a user-defined method that accepts a parameter:

static void showAge(int age) {
  System.out.println("Employee Age: " + age);
}

In the above snippet, showAge() is the user-defined method that takes an integer type parameter. The static keyword shows that the method “showAge()” can be approached directly using the class name. Void keyword shows that the showAge() method doesn’t return anything.

The parameters will be passed to a method at the time of method calling as shown in the below given snippet:

showAge(32);

The actual parameters(values) passed to the method at the time of method calling are known as the arguments.

Now let’s conclude this example to figure out what’s the key difference between arguments and parameters in java.

From the example given above, we come to know that the actual parameters/values that are passed to a method at the time of method calling are known as arguments (in this example 32 is an argument) while the parameters are the variables defined in the method’s header (in this example int age is a parameter).

How to pass a single parameter to a method in java

In this example, the user-defined method “printName()” utilizes only one parameter of integer data type. From the main method we passed two arguments to the printName() method. Following will be the resultant output for the above given java program:

public class ParametersExample{
  static void printName(String userName) {
    System.out.println("User Name: " + userName);
  }

  public static void main(String[] args) {
    printName("Ambrose");
    printName("David");
     }
}

This is how we pass a parameter to a method.

How to pass multiple parameters to a method in java

The following snippet shows how to use comma-separated syntax to pass more than one argument to a method:

package parametersexample;
public class ParametersExample {
    static void printName(String userName, int userAge) {
        System.out.println("Employee Name: " + userName);
        System.out.println("Employee Age: " + userAge);
    }

    public static void main(String[] args) {
        printName("Ambrose", 34);
        printName("David", 24);
    }
}

The output verifies the working of the above given java program.

How to pass parameters to a constructor in java

In this example, we have a constructor that accepts two parameters of integer data type. Afterward, it calculates the product of those numbers:

package parametersexample;
public class ParametersExample {
    ParametersExample(int x, int y)
    {
        System.out.println("Product of two numbers: " + x*y);
    }

    public static void main(String[] args) {
        ParametersExample object = new ParametersExample(3, 23);
    }
}

We passed the arguments to the constructor from the main method. The constructor accepted the parameters and performed the desired functionality on them.

Conclusion

The parameters in java are variables that can be passed to the methods or constructors. Within these methods or constructors these parameters act as variables. The actual parameters/values passed to a method at the time of method calling are known as arguments while the parameters are the variables defined in the method’s header. This post explained various aspects of parameters in java with the help of some suitable examples.

About the author

Anees Asghar

I am a self-motivated IT professional having more than one year of industry experience in technical writing. I am passionate about writing on the topics related to web development.