Passing parameters by values in Java

One of the key features of method is it’s ability to work with parameters. Whenever you are calling a method  you should always provide arguments that must be in the same order as their respective parameters  in method definition. This is actually known as parameter order association. The arguments must match the parameters in the order, compatible type and number as defined in method definition(signature). When a method with a parameter is invoked the value of the argument  is passed to that parameter. this is known as passing parameters by value. The variable is not affected regardless of the changes that are made to the parameter inside the method.

As for example we can see in the below example that the value of “a” is passed to the parameter number to invoke the addHundred method and hence the number increases by 100 but the value of “a” remains the same no matter what the  method does.


public class PassingParameters {
public static void main(String[] args){

int a = 5;
System.out.println("The value of a before calling the method is: "+a);

addHundred(a); //method is invoked

System.out.println("The value of a after calling the addHundred method is: "+a);

}
public static void addHundred(int number){
number = number+100;
System.out.println("The value of number inside the method is : "+number);
}

}

OUTPUT:
The value of a before calling the method is: 5
The value of number inside the method is : 105
The value of a after calling the addHundred method is: 5

Lets do another example of swapping the numbers. The method Swap() is used to swap the two numbers but it swaps the values inside the Swap() method. It does not have any impact to the values outside of it.


public class Swap{
public static void Swap(int a, int b){
System.out.println("Inside the Swap Method: Before Swapping the first and second numbers are :"+a+" and "+b);

int temp = a;
a=b;
b=temp;

System.out.println("Inside the Swap Method: After Swapping the first and second numbers are :"+a+" and "+b);

}

public static void main(String[] args){
int number1 = 200;
int number2 = 600;
System.out.println("Before invoking the Swap method first number is "+number1+" and "+"second number is "+number2);
Swap(number1, number2); //method is invoked
System.out.println("After invoking the Swap method first number is "+number1+" and "+"second number is "+number2);

}

}

OUTPUT:
Before swapping first number is 200 and second number is 600
The first and second numbers are :600 and 200
After invoking the Swap method first number is 200 and second number is 600

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s