Example of pass by value:
public class Passbyvalue {
public static void main(
String[] args) {
int a=15,b;
System.out.println("Value of a before call:"+a);
Value value = new Value();
value.meth(a);
System.out.println("Value of a after call:"+a);
}
}
public class Value {
void meth(int i){
i *= i;
}
}
Here the value of a remains as 15.
If i perform same operation with
return value the value of a is changed.
public class Passbyvalue {
public static void main(String[] args) {
int a=15,b;
System.out.println("Value of a before call:"+a);
Value value = new Value();
value.meth(a);
b= value.meth(a);
System.out.println("Value of a after call:"+b);
}
}
public class Value {
int meth(int i){
i *= i;
return i;
}
}
here value of a is changed to 255.It is pass by value and it changes the value.
Can you explain it HOW?