Passing object as parameter into function
Please read following code.
public class Test {
public static void main(String[] args) {
// Integer reference called "clz"
Integer clz = new Integer(1);
changeValue(clz);
System.out.println("After called changeValue(): " + clz.toString());
}
public static void changeValue(Integer input) {
System.out.println("Start changeValue(): " + input);
input = new Integer(2);
System.out.println("Finish changeValue(): " + input);
}
}
The output is
Start changeValue(): 1
Finish changeValue(): 2
After called changeValue(): 1
Finding
- Changing the value of reference (ie create a new object) inside a function will not affect the original value of reference passed into
- Before function is executed in JVM, value of reference "clz" copy to stack memory space "input". During execution, the change is on that stack memory space and after function is executed value of stack memory space will not copy back to original
Reference
Author And Source
この問題について(Passing object as parameter into function), 我々は、より多くの情報をここで見つけました https://qiita.com/saladlam/items/4759cb6ff0bb4b4d0abc著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .