Below example will explain how call by value is happening in java.
Output:
public class CallByValue {
private int value = 5;
private int change(int input) {
input = input + 10;// changes will be in the local variable only
return input;
}
public static void main(String args[]) {
CallByValue cv = new CallByValue();
System.out.println("before change: " + cv.value);
System.out.println("calling change method directly: " + cv.change(50));
System.out.println("after change: " + cv.value);
}
}
Output:
before change: 5
calling change method directly: 60
after change: 5
No comments:
Post a Comment