Java Method Parameters

Method Parameters:

Method parameters are variables declared in the method signature, which act as placeholders to accept input values when the method is called. They define what type of data the method expects to receive as input. Parameters allow methods to be more flexible and reusable, as different values can be passed to the method each time it is invoked.

The syntax for method parameters:

// method with parameters

accessModifier returnType methodName(parameterType1 parameterName1, parameterType2 parameterName2, …) {

// method body

}

Method Arguments:

Method arguments are the actual values that are passed to a method when it is called. They are the concrete values that are provided to the method’s parameters. When a method is called, arguments are passed inside the parentheses, matching the order and type of the method’s parameters.

The syntax for method arguments:

methodName(argumentValue1, argumentValue2, …);

Example of a method with parameter and argument:

public class SquareCalculator {
    // Method to calculate the square of a number
    public static int square(int number) {
        return number * number;
    }

    public static void main(String[] args) {
        int num = 5; // Argument value
        int result = square(num); // Calling the 'square' method with the argument num
        System.out.println("The square of " + num + " is: " + result);
    }
}

Explanation:

  1. public class SquareCalculator { … }: This is the declaration of the class named SquareCalculator. It contains two methods: ‘square’ and ‘main’.
  2. public static int square(int number) { … }: This is the ‘square’ method, which is used to calculate the square of a number. It takes one parameter of type int named number.
  3. return number * number;: Inside the square method, it calculates the square of the input number by multiplying it by itself and returns the result, which is an integer.
  4. public static void main(String[] args) { … }: This is the main method, which is the entry point of the Java program. It is where the program execution starts.
  5. int num = 5;: In the main method, a variable num of type int is declared and initialized with the value 5. This value will be used as the argument when calling the square method.
  6. int result = square(num);: This line calls the square method and passes the value of the num variable as an argument. The method will compute the square of the number num (which is 5) and return the result, which will be stored in the result variable of type int.
  7. System.out.println(“The square of ” + num + ” is: ” + result);: Finally, this line prints the result of the square calculation to the console. It uses string concatenation to display the original number (num) and its square (result) in the output message.
Parameter and Argument:
  1. Parameter: In the method signature public static int square(int number), the parameter is defined as int number. It specifies that the method square expects an input value of type int, which will be referred to as number inside the method body.
  2. Argument: In the main method, when we call the square method using square(num), we pass the value of the variable num (which is 5) as an argument to the square method. This value is used as the number parameter in the square method for calculating the square.

Output :

The square of 5 is: 25

Multiple Parameters:

Let’s build a Java program that uses a method with multiple parameters We will define a method called calculateSum that takes two integers as parameters, and we will use specific arguments to calculate the sum of two numbers using this method:

public class Calculator {
    // Method to calculate the sum of two integers
    public static int calculateSum(int num1, int num2) {
        return num1 + num2;
    }

    public static void main(String[] args) {
        int a = 10; // First argument value
        int b = 20; // Second argument value
        int sum = calculateSum(a, b); // Calling the 'calculateSum' method with the arguments a and b
        System.out.println("The sum of " + a + " and " + b + " is: " + sum);
    }
}

Explanation:

  1. public class Calculator { … }: This is the declaration of the class named Calculator. It contains two methods: calculateSum and main.
  2. public static int calculateSum(int num1, int num2) { … }: This is the calculateSum method, which is used to calculate the sum of two integers. It takes two parameters of type int named num1 and num2.
  3. return num1 + num2;: Inside the calculateSum method, it calculates the sum of the two input numbers num1 and num2 and returns the result, which is an integer.
  4. public static void main(String[] args) { … }: This is the main method, which is the entry point of the Java program. It is where the program execution starts.
  5. int a = 10;: In the main method, a variable a of type int is declared and initialized with the value 10. This will be used as the first argument when calling the calculateSum method.
  6. int b = 20;: In the main method, another variable b of type int is declared and initialized with the value 20. This will be used as the second argument when calling the calculateSum method.
  7. int sum = calculateSum(a, b);: This line calls the calculateSum method and passes the values of variables a and b as arguments. The method will compute the sum of the two numbers a and b (which are 10 and 20) and return the result, which will be stored in the sum variable of type int.
  8. System.out.println(“The sum of ” + a + ” and ” + b + ” is: ” + sum);: Finally, this line prints the result of the sum calculation to the console. It uses string concatenation to display the original numbers (a and b) and their sum (sum) in the output message.

Multiple Parameters and Arguments:

In the calculateSum method, we defined two parameters num1 and num2. When calling the calculateSum method in the main method, we passed two arguments a and b as inputs. These arguments match the order and type of the parameters in the method’s signature. The method then uses these arguments to perform the addition and return the sum, which is then printed to the console.

Output :

The sum of 10 and 20 is: 30

 

Visited 1 times, 1 visit(s) today

Comments are closed.

Close