Call by value Vs Call by Reference

Call by value Vs Call by Reference:

Call By Value Vs Call By reference

Call By Value Vs Call By Reference

Call by value:

public class CallBy_Value
{
public static void main(String[] args)
{
Value v = new Value(10,20);
System.out.println("a and b before call............");
System.out.println("a = "+v.a);
System.out.println("b = "+v.b);
v.call(v.a,v.b); // CALL BY VALUE
System.out.println("a and b after call............");
System.out.println("a = "+v.a);
System.out.println("b = "+v.b);
}
}


class Value
{
int a,b;
Value(int i,int j)
{
a = i ;
b = j;
}
void call(int a, int b)
{
a = a * 2;
b = b * 2;
}
}

Call by Reference:

public class CallBy_Reference
{
public static void main(String[] args)
{
Reference r = new Reference(10,20);
System.out.println("a and b before call............");
System.out.println("a = "+r.a);
System.out.println("b = "+r.b);
r.call(r); // CALL BY REFERENCE
System.out.println("a and b after call.............");
System.out.println("a = "+r.a);
System.out.println("b = "+r.b);
}
}


class Reference
{
int a,b;
Reference(int i,int j)
{
a = i ;
b = j;
}
void call(Reference r)
{
r.a = a * 2;
r.b = b * 2;
}
}

Learn With theory and output of above examples.
Call by value in java and Call by Reference in java

Advertisements

About akjava2all

Learn java and advance java online with simple java code, java tutorial, java example and java ppt.
This entry was posted in Core Java and tagged , , . Bookmark the permalink.

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