[Solved] Whats the difference between java parameters and clarifying it within the method


Parameters allows a method to have more flexibility. Sometimes it is necessary to run a method but with different arguments, this is where parameters become handy.

For Example (Clarifying):

public void calcTotal()
{
    int firstNum= 1;
    int secondNum=2;

    System.out.println(firstNum+secondNum);

    //when we run calcTotal()
    //output= 3

}

This method would correctly print the sum of the two numbers. However, this method will only allow us to print the sum of 1+2. What happens when we want to find the sum of another set of two integers i.e 5 & 6?

Would we give this task to another method by copying the first one and changing the values of firstNum & secondNum?

public void calcTotal()
{
    int firstNum= 5;
    int secondNum=6;

    System.out.println(firstNum+secondNum);

    //when we run calcTotal()
    //output= 3
}

The Answer is NO.

By allowing a method to take arguments… We can…

public void calcTotal(int firstNum, int secondNum)
{
    System.out.println(firstNum+secondNum);
}

With a method that can take arguments… We can…

public static void main(String args[])
{
    calcTotal(1,2); //output 3
    calcTotal(5,6); //11
    calcTotal(6,7); //12
}

calculate the sum of different pairs of integers without creating another method for each set of integers.

0

solved Whats the difference between java parameters and clarifying it within the method