Core Java

Parameter vs Argument in Java

Hello. In this tutorial, we will talk about Parameter vs Argument in Java.

1. Introduction

1.1 What is an argument in Java?

In Java, an argument refers to the value that is passed to a method when it is called. When you define a method, you can specify one or more parameters in its signature to receive values. When you invoke or call that method, you pass the corresponding arguments that match the parameter types.

1.2 What is a parameter in Java?

In Java, a parameter refers to a variable that is declared in the method signature and is used to receive values when the method is called. Parameters define the inputs that a method expects to perform its task. They act as placeholders for the actual values that will be passed as arguments when invoking the method.

2. Examples

2.1 Argument example

Here’s an example of a method with a single argument in Java:

Sample code

public void greet(String name) {
    System.out.println("Hello, " + name + "!");
}

In the above example, the method greet accepts a single argument of type String named name. When you call this method, you need to provide a String value as an argument:

greet("John");

In this case, “John” is the argument being passed to the greet method. The method will then execute and print “Hello, John!” to the console.

You can have multiple arguments in a method, separated by commas. The order and type of the arguments should match the method’s parameter list. Arguments allow you to provide data to a method that it needs to perform its task. They allow for flexibility and reusability by enabling different values to be passed to the same method.

2.2 Parameter example

Here’s an example of a method with parameters in Java:

Sample code

public void addNumbers(int a, int b) {
    // a and b are the parameters
    int sum = a + b;
    System.out.println("The sum is: " + sum);
}

In the above example, the method addNumbers has two parameters, a and b, both of type int. These parameters are variables that will store the values passed as arguments when the method is called. Inside the method, the parameters can be used like any other variable.

When you invoke the addNumbers method, you need to provide two int values as arguments, which will be assigned to the corresponding parameters:

addNumbers(5, 7);

In this case, 5 will be assigned to the parameter a, and 7 will be assigned to the parameter b. The method will then calculate the sum of the two numbers and print “The sum is: 12” to the console.

Parameters allow methods to receive data from the caller, enabling the method to perform operations or computations on the provided values. They provide a way to make methods more flexible and reusable by accepting different inputs.

3. Difference between Parameter vs Argument

Parameter Argument
Parameters are defined in the method/function signature or declaration. Arguments are the actual values passed to the method/function when it is called/invoked.
Parameters act as placeholders or named variables to receive values. Arguments are the concrete values that fulfill the parameters.
Parameters are part of the method/function definition and provide information about the type and name of expected values. Arguments are the values provided in a specific order to match the corresponding parameters.
Parameters are used within the method/function’s body as local variables to perform computations or operations. Arguments are passed from the calling code to provide specific data for the method/function to work with.
Parameters define the contract or interface of the method/function, indicating what inputs are expected. Arguments satisfy the contract by providing the necessary values to the method/function.

4. Conclusion

In Java, parameters and arguments play essential roles in method definitions and invocations. Parameters are declared within the method signature and act as placeholders for expected values. They provide information about the type and name of the inputs that a method expects to receive. On the other hand, arguments are the actual values that are passed to a method when it is called. They correspond to the parameters defined in the method’s signature and fulfill the method’s input requirements.

Parameters allow methods to be flexible and reusable by accepting different inputs, while arguments provide the specific data necessary for the method to perform its task. By properly matching arguments to parameters, developers can ensure that methods receive the correct values to produce desired results.

Understanding the distinction between parameter vs argument is crucial for writing effective and functional code. Parameters define the contract of a method, specifying the expected inputs, while arguments fulfill that contract by providing the necessary values. By utilizing parameters and arguments effectively, developers can create modular and adaptable code that can handle a wide range of scenarios.

Yatin

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button