Java Call By Value
Chapter:
Miscellaneous
Last Updated:
23-03-2017 19:56:20 UTC
Program:
/* ............... START ............... */
public class JavaCallByValue {
int number = 100;
void changeNumber(int number) {
number = number + 100;
System.out.println("Number in changeNumber function : " + number);
}
public static void main(String args[]) {
JavaCallByValue callByValueDemo = new JavaCallByValue();
System.out.println("Before changing the number : " + callByValueDemo.number);
callByValueDemo.changeNumber(callByValueDemo.number);
System.out.println("After changing the number : " + callByValueDemo.number);
}
}
/* ............... END ............... */
Output
Before changing the number : 100
Number in changeNumber function : 200
After changing the number : 100
Notes:
-
Call By Value – is when primitive data types are passed in the method call, so pushing their values on stack.
- Java does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made in one reference variable are visible through the other variable.
Tags
Call By Value, Java, Miscellaneous