Swap Program demonstrating Call by Value in Java

//Program demonstrating Call by Value in Java

SwapCallByValDemo.java

class Test

{

                void swap(int a, int b)

                {

                                int c;

                                c=a;

                                a=b;

                                b=c;

                                System.out.println(“Inside swap method”);

                                System.out.println(“a = “+a+” b = “+b);

                }

}

class SwapCallByValDemo

{

                public static void main(String[] args)

                {

                                Test ob1 = new Test();

                                int x, y;

                                x=2;

                                y=5;

                                System.out.println(“Before Swapping”);

                                System.out.println(“x = “+x+” y = “+y);

                                ob1.swap(x,y);

                                System.out.println(“After Swapping in main method”);

                                System.out.println(“x = “+x+” y = “+y);

                }

}

Advertisements

One thought on “Swap Program demonstrating Call by Value in Java

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