Passing Parameters by Value

0
287
Java Tutorial: Passing Parameters by Value

1Passing Parameters by Value:

While working under calling process, contentions is to be passed. These ought to be in an indistinguishable request from their particular parameters in the strategy detail. Parameters can be passed by esteem or by reference.
Passing Parameters by Value implies calling a technique with a parameter. Through this, the contention esteem is passed to the parameter.

Illustration

The accompanying project demonstrates a case of passing parameter by esteem. The estimations of the contentions continues as before even after the technique conjuring.

Example:

The below program shows an example of shifting parameter by value. The values of the arguments not change even after the method invocation.

public class swappingExample {
public static void main(String[] args) {
int a = 30;
int b = 45;
System.out.println(“Before swapping, a = “ + a + ” and b = “ + b);
// Invoke the swap method
swapFunction
(a, b);
System.out.println(“\n**Now, Before and After swapping values will be same here**:”);
System.out.println(“After swapping, a = “ + a + ” and b is “ + b);
}
public static void swapFunction(int a, int b) {
System.out.println(“Before swapping(Inside), a = “ + a + ” b = “ + b);
// Swap n1 with n2
int c = a;
a
= b;
b
= c;
System.out.println(“After swapping(Inside), a = “ + a + ” b = “ + b);
}
}

This will gives the following output:

Output

Before swapping, a = 30 and b = 45
Before swapping(Inside), a = 30 b = 45
After swapping(Inside), a = 45 b = 30

**Now, Before and After swapping values will be same here**:
After swapping, a = 30 and b is 45

Method Overloading:

Method overloading is defined as a method if any method contain the same name but different parameters. Under overriding, a method has same name, number of parameters same type etc.

Let’s take the example that is discussed earlier for identifying the minimum numbers of integer type. If it says that we want to search for the minimum but of double type. Then this concept of overloading will be come to create two or more than two methods with different parameters and same name.

This example will explain the same:

Example

public class ExampleOverloading {
public static void main(String[] args) {
int a = 11;
int b = 6;
double c = 7.3;
double d = 9.4;
int result1 = minFunction(a, b);
// same function name with different parameters
double result2 = minFunction(c, d);
System.out.println(“Minimum Value = “ + result1);
System.out.println(“Minimum Value = “ + result2);
}
// for integer
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min
= n2;
else
min
= n1;
return min;
}
// for double
public static double minFunction(double n1, double n2) {
double min;
if (n1 > n2)
min
= n2;
else
min
= n1;
return min;
}
}

The result will be following:

Output

Minimum Value = 6
Minimum Value = 7.3

The overloading methods makes the program readable. Here are two methods that are provided by the different parameters but of same name. The small number from integer or double types will be the result.

BackNext