Parameters & Arguments in Java

Table of contents

No heading

No headings in the article.

In Java, a parameter refers to the expected value that is passed into a method or constructor when it is invoked. Parameters allow you to provide data to a method so that it can perform its intended functionality. They are specified in the method or constructor declaration. They typically serve as placeholders for the values that will be passed when the method is called.

Here's an example showing what I mean by a method declaration with parameters:

public int addNumbers( int x, int y){

        int add = x + y;

        return add;
    }

In this example, the addNumbers method has two parameters called x and y, which are of type Integer. When the addNumbers method is called upon, the caller needs to provide an integer value for the addNumbers parameter, like this:

operations.addNumbers(5,6);

Argument

The values 5 and 6 provided by the caller are termed "Arguments". Now, in this case, the values "5 and 6" will be assigned to the addNumbers parameter within the method, and the output will be:

output 
The sum is: 11

In conclusion, Parameters and Arguments both work hand in hand and can be likened to the search bar on Google, it's expecting a search query from you so it can give you a search result.