Day24 : Argument Passing

Argument Passing

 In Java we can pass two types of arguments to a method and they are

  1. Variables (Primitive Datatypes)
  2. References (Array References & Object Reference

Passing Variable

Whenever we pass any variable as argument to a method then java passes its value. This value is received  by the formal argument declared in the method's argument list. So now both the argument, actual and formal have the same value but their address are different so if we make any change in the value of formal argument then it will never effect the value of actual argument. Thus we say that in Java variables are always passed using pass by value.

The Second type of argument which we can pass to any method in Java is reference (Array Reference and Object Reference). Whenever we pass any reference as argument to a method then Java passes the addresses of the object or array pointed by the reference. This address is then received by formal reference declared in the argument list of the method. so now both the references, actual and formal, point to the same object. Now if we will change the value of the object using formal reference they it will surely change the value of the original and due to this many beginners in Java start thinking that it pass by reference But this is not true.

This is because if we will change the formal reference itself it will neither effect original object nor the actual references. Thus even reference are passed using pass by value

So the final conclusion is that Java doesn't support pass by reference and everything is passed using pass by value.

Why Java being such a powerful language, doesn't support pass by references?

Java is a language which is primarily used for developing web application and is very popular in banking and insurance sector. Now if the developers of java supported pass by reference then it would have been a security threat for Java application running on servers because using pass by reference we can easily manipulate original value. So in the favor of security the developers of Java decided not to support pass by reference.

Example : 

class Num
{
private int a,b;
{
public void setNum(int i, int j)
a = i;
b = j;
}
public void showNum()
{
System.out.println("a = "+a + ",b = "+b)
}
public void increment(Num P)
{
P.a++;
P.b++;
}
}

class UseNum
{
public static void main(String [ ] args)
{
Num N = new Num();
N.setNum(10,20);
System.out.println("Before Incrementing : ");
N.showNum();
Num Temp = new Num();
System.out.println("After Incrementing : ");
N.showNum();
}
}

Write a program to swap two Integer data member of the class. Later you can modify it to accept input from the user.

File Name : Num.Java
public class Num
{
private int a, b;
public void setNum(int i, int j)
{
a = i;
b = j;
}
public void showNum(){
System.out.println("a = "+a+",b = "+b);
}
public void swap(Num P, Num Q)
{
int x;
x = P.a;
P.a = Q.a;
Q.a = x;
x = P.b;
P.b = Q.b;
Q.b = x;
}
}

File Name : UseNum.java

public class UseNum
{
public static void main(String[] args) {
Num N1 = new Num();
Num N2 = new Num();

N1.setNum(10,20);
N2.setNum(30,40);
System.out.println("Before Swapping: ");
N1.showNum();
N2.showNum();

Num Temp = new Num();
Temp.swap(N1,N2);
System.out.println("After Swapping: ");
N1.showNum();
N2.showNum();
}
}
Output : 
Before Swapping: 
a = 10,b = 20
a = 30,b = 40
After Swapping: 
a = 30,b = 40
a = 10,b = 20

Passing Array Reference as Argument to method

<access_mod><ret_type><method_name>(<datatype> [ ] <arr_ref>)
    {
        //Method Body
    }

Exercise : 
Problem1 : Write a program to create MyMath having a method called sum() which should accept an integer array as argument and should return the sum of all the element of that array.
Now design the driver class called UseMyMath. Declare an integer array of 5 element accept Input from from the user in that array and using the method sum() of MyMath class, calculate and display the sum of array element.

File Name : MyMath.java

public class MyMath
{
public int sum(int [ ] brr)
{
int total = 0;
for(int x : brr)
{
total += x;
return total;
}
}
}

File Name : UseMyMath.java

import java.util.Scanner;
public class UseMyMath
{
public static void main(String [ ] args)
{
int [ ] arr = new int[5];
Scanner kb = new Scanner(System.in);
arr[i] = kb.nextInt();
}
MyMath obj = new MyMath();
int total;
total = obj.sum(arr);
System.out.println("sum is "+total);
}


Problem 2 : Modify the problem 1 by making following changes in MyMath Class
  1. Rename the method sum() to calculate()
  2. The method calculate should new return sum as well as average of array data passed as argument.
  3. Finally the driver class must display it.

Returning Array from Method

File Name : MyMath.java

public class MyMath
{
public double [ ] calculate(int [ ] brr)
{
int total = 0;
double [ ] result = new double[2];
for(int x : brr)
{
total += x;
return[0] = total;
return[1] = (float)total/brr.length;
return result;
}
}
}

File Name : UseMyMath.java

import java.util.Scanner;
public class UseMyMath
{
public static void main(String [ ] args)
{
int [ ] arr = new int[5];
Scanner kb = new Scanner(System.in);
for(int i = 0; i<arr.length; i++)
{
System.out.println("Enter No ");
arr[i] = kb.nextInt();
}
MyMath obj = new MyMath();
double [ ] ans;
ans = obj.calculate(arr);
System.out.println("sum is "+ans[0]);
System.out.println("Avg is " + ans[1]);
}

Comments

Popular posts from this blog

Day2 : Compiling Java Program with Examples

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?