In: Computer Science
Before call obj.a 15 obj.b is 20
Before call obj.a 30 obj.b is 10
CallValueTest.java
package com.Assignment3.Q1;
class Test {
void calc(int a, int b) {
a *= 2;
b /= 2;
System.out.println(" in calc a = "
+ a + " b = " + b);
}
}
public class CallByValueTest {
public static void main (String args[]) {
Test ob = new Test();
int a =15, b = 20;
System.out.println ("Before calc :a
= " + a + " b " + b);
ob.calc (a, b );
System.out.println(" After calc: a
= " + a + " b " + b);
}
}
//Java code
package com.Assignment3.Q1; /** * Since in call by value, We are passing primitive type(int, char..) Which supports pass by value. * To convert the it into call by reference, We need to wrap them into reference type (Object). */ class Test { //attributes int a,b; void calc() { a *= 2; b /= 2; System.out.println(" in calc a = " + a + " b = " + b); } } public class CallByRefTest { public static void main (String args[]) { Test ob = new Test(); ob.a=15; ob.b =20; System.out.println ("Before calc :a = " + ob.a + " b " + ob.b); ob.calc ( ); System.out.println(" After calc: a = " + ob.a + " b " + ob.b); } }
//output
//If you need any help regarding this solution ......... please leave a comment ...... thanks