Passing Parameters by Value | W3schools | Tutorials Point | W3school

Passing 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.

Using Command-Line Arguments:

While you are running a program there are some necessary informations which you have to transfer. This can be only possible by passing command-line arguments() to main.
It is a information that straight away follows the name of the programs when executed on the command. Accesing it in java program is simple. As they are kept as strings then in it straightly passed to main.

Example

The following program shows all of the command-line arguments that it is known with −

public class CommandLine {
public static void main(String args[]) {
for(int i = 0; i<args.length; i++) {
System.out.println(“args[“ + i + “]: “ + args[i]);
}
}
}

Try to execute this plan as shown below:

$java CommandLine this is a command line 200 -100

$java CommandLine this is a command line 200 -100
This will produce the following result −

Output
args[0]: this
args[1]: is
args[2]: a
args[3]: command
args[4]: line
args[5]: 200
args[6]: -100

Here is a quite simple example that uses a constructor without parameters −

// A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass() {
x
= 10;
}
}

You need to call constructor to set the objects as follows −

public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass();
MyClass t2 = new MyClass();
System.out.println(t1.x + ” “ + t2.x);
}
}

Output

10 10

Parameterized Constructor:

So many times you must need a constructor that can accept more than two parameters. The process of adding parameters to a constructor is same as adding to a method.

Example

This example show the se of parameter within the constructor:

// A simple constructor.
class MyClass {
int x;
// Following is the constructor
MyClass(int i ) {
x
= i;
}
}

Again you are required to call a constructor in order to initialize the objects

public class ConsDemo {
public static void main(String args[]) {
MyClass t1 = new MyClass( 10 );
MyClass t2 = new MyClass( 20 );
System.out.println(t1.x + ” “ + t2.x);
}
}

The following result will be produced:

Output

10 20