Home | | Sitemap ||Page number :10

Passing Values to methods and Constructors

Now you are familiar with passing values to methods. We have passed integers(data types) as well as objects. These are two different ways of supplying values to methods. Classified under these two titles - 1.Pass by Value 2.Pass by Address or Reference

Pass by Value-When we pass a data type(like a variable “num” of int, float or any other type of data) to a method or some constant values like(15,10). They are all passed by value. A copy of variable’s value is passed to the receiving method and hence any changes made to the values do not affect the actual variables.

Consider this example---

class pass_by_val { int n,n2;

public void get(int x,int y) { x=x*x; //Changing the values of passed arguments y=y*y; //Changing the values of passed arguments }

}

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

int a,b; a=1; b=2; System.out.println("Initial Values of a & b "+a+" "+b);

pass_by_val obj=new pass_by_val(); obj.get(a,b);

System.out.println("Final Values "+a+" "+b); }

}

g

Pass by Reference

Objects are always passed by reference. When we pass a value by reference, the reference or the memory address of the variables is passed. Thus any changes made to the argument causes a change in the values which we pass.

Demonstrating Pass by Reference---

class pass_by_ref { int n,n2; public void get(int a,int b) { n=a; n2=b; }

public void doubleit(pass_by_ref temp) { temp.n=temp.n*2; temp.n2=temp.n2*2; }

}

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

{

int x=5,y=10;

pass_by_ref obj=new pass_by_ref(); obj.get(x,y); //Pass by Value

System.out.println("Initial Values are-- "); System.out.println(+obj.n); System.out.println(+obj.n2);

obj.doubleit(obj); //Pass by Reference System.out.println("Final Values are"); System.out.println(+obj.n); System.out.println(+obj.n2); }

g

Pure & Impure Functions

Pure Functions --Pure functions do not modify their arguments or output. The result of a pure function call is the return value. Impure Functions—Just opposite to pure function.

Recursion

When a method calls itself, it is known as recursion. This phenomenon may look weird but it becomes really helpful in some conditions. The power of recursion can be demonstrated by taking an example of a class that calculates factorial of a number. If you are unfamiliar with the term “factorial”, here is the description with an example.

No=5

Factorial=5*4*3*2*1 You can deduce from this example, the factorial of a number ‘N’ is the product of all natural numbers descending from ‘N’ till 1.

class fact {

int ans;

public int take(int no) { if(no==1) return 1; ans=take(no-1)*no; return ans; } }

class apply8 { public static void main(String args[]) { fact obj=new fact(); System.out.println(+obj.take(5)); } }

The method calls itself repeatedly and calculates the factorial. If you want to see the stepwise calculation of the factorial, insert a print statement in the method. You will then understand its working better.