Java中方法的参数传递均为按值传递,即使传递的是对象或者数组,传递过去的也是引用的拷贝。
package com.algorithms;
/**
* Created by 田爽 on 2015/9/27 9:16.
*/
public class Person {
private int age;
public Person(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
'}';
}
}
package com.algorithms;
/**
* Created by 田爽 on 2015/9/15 19:49.
*/
public class Test {
public static void main(String[] args) {
Person person = new Person(10);
transmit(person);
System.out.println(person);
}
private static void transmit(Person person) {
person = new Person(20);
}
}