Program to Demonstrate Call by Value and Call by Reference in Java (BlueJ)
A) Program to Demonstrate Call by Reference
code:
OUTPUT:
***** Program to demonstrate call by value *****
Value of num1 before call to swap :100
Value of num2 before call to swap :200
***********************************************
Value of num1 in swap :200
Value of num2 in swap :100
***********************************************
Value of num1 after call to swap :200
Value of num2 after call to swap :100
***********************************************
A) Program to Demonstrate Call by Value
Code:
OUTPUT:
***** Program to demonstrate call by value *****
Value of num1 before call to swap :100
Value of num2 before call to swap :200
Value of num1 in swap :200
Value of num2 in swap :100
Value of num1 after call to swap :100
Value of num2 after call to swap :200
A) Program to Demonstrate Call by Reference
code:
/***** PROGRAM TO DEMONSTRATE CALL BY REFERENCE *****/
public class CallByRef
{
public static int num1 = 100, num2 = 200;
public static void main()
{
CallByRef cbr = new CallByRef();
System.out.println("***** Program to demonstrate call by reference *****");
System.out.println("Value of num1 before call to swap :"+cbr.num1);
System.out.println("Value of num2 before call to swap :"+cbr.num2);
System.out.println("***********************************************");
swap(cbr); // calling swap method by reference
System.out.println("Value of num1 after call to swap :"+cbr.num1);
System.out.println("Value of num2 after call to swap :"+cbr.num2);
System.out.println("***********************************************");
}
public static void swap (CallByRef cbr2)
{
int temp;
temp = cbr2.num1;
cbr2.num1 = cbr2.num2;
cbr2.num2 = temp;
System.out.println("Value of num1 in swap :"+cbr2.num1);
System.out.println("Value of num2 in swap :"+cbr2.num2);
System.out.println("***********************************************");
}
}
public class CallByRef
{
public static int num1 = 100, num2 = 200;
public static void main()
{
CallByRef cbr = new CallByRef();
System.out.println("***** Program to demonstrate call by reference *****");
System.out.println("Value of num1 before call to swap :"+cbr.num1);
System.out.println("Value of num2 before call to swap :"+cbr.num2);
System.out.println("***********************************************");
swap(cbr); // calling swap method by reference
System.out.println("Value of num1 after call to swap :"+cbr.num1);
System.out.println("Value of num2 after call to swap :"+cbr.num2);
System.out.println("***********************************************");
}
public static void swap (CallByRef cbr2)
{
int temp;
temp = cbr2.num1;
cbr2.num1 = cbr2.num2;
cbr2.num2 = temp;
System.out.println("Value of num1 in swap :"+cbr2.num1);
System.out.println("Value of num2 in swap :"+cbr2.num2);
System.out.println("***********************************************");
}
}
OUTPUT:
***** Program to demonstrate call by value *****
Value of num1 before call to swap :100
Value of num2 before call to swap :200
***********************************************
Value of num1 in swap :200
Value of num2 in swap :100
***********************************************
Value of num1 after call to swap :200
Value of num2 after call to swap :100
***********************************************
A) Program to Demonstrate Call by Value
Code:
/***** PROGRAM TO DEMONSTRATE CALL BY VALUE *****/
public class CallByVal
{
public static void main()
{
int num1 = 100, num2 = 200;
System.out.println("***** Program to demonstrate call by value *****");
System.out.println("Value of num1 before call to swap :"+num1);
System.out.println("Value of num2 before call to swap :"+num2);
swap(num1, num2); // calling swap method by value
System.out.println("Value of num1 after call to swap :"+num1);
System.out.println("Value of num2 after call to swap :"+num2);
}
public static void swap (int num11,int num22)
{
int temp;
temp = num11;
num11 = num22;
num22 = temp;
System.out.println("Value of num1 in swap :"+num11);
System.out.println("Value of num2 in swap :"+num22);
}
}
public class CallByVal
{
public static void main()
{
int num1 = 100, num2 = 200;
System.out.println("***** Program to demonstrate call by value *****");
System.out.println("Value of num1 before call to swap :"+num1);
System.out.println("Value of num2 before call to swap :"+num2);
swap(num1, num2); // calling swap method by value
System.out.println("Value of num1 after call to swap :"+num1);
System.out.println("Value of num2 after call to swap :"+num2);
}
public static void swap (int num11,int num22)
{
int temp;
temp = num11;
num11 = num22;
num22 = temp;
System.out.println("Value of num1 in swap :"+num11);
System.out.println("Value of num2 in swap :"+num22);
}
}
OUTPUT:
***** Program to demonstrate call by value *****
Value of num1 before call to swap :100
Value of num2 before call to swap :200
Value of num1 in swap :200
Value of num2 in swap :100
Value of num1 after call to swap :100
Value of num2 after call to swap :200
Um why don't u give explanations to the topics?
ReplyDelete