对象a作为参数argument在方法中使用时,如果argument在方法中赋予另一个对象的地址,则之后方法中对参数argument的操作,都不会影响到对象a.
方法中参数argument如果是对象,argument是源对象a的副本,方法执行结束,副本argument就被移出stack,在合适的时间就会被GC回收
/*2015-7-22*/ public class ReferenceTypeDemo { public static void main(String[] args) { Person tom = new Person("Tom", 20); System.out.println(tom); modify(tom); System.out.println(tom); } private static void modify(Person tom) { System.out.println("modify():" + tom); Person jack = new Person("Jack", 10); tom = jack; System.out.println(tom); System.out.println("modify():" + tom); tom.setName("Tom-modify"); System.out.println("modify():" + tom); } } class Person { private String name; private int age; public Person(String name, int age) { super(); this.name = name; this.age = age; } public void setName(String name) { this.name = name; } @Override public String toString() { return Integer.toHexString(hashCode()) + ";Person [name=" + name + ", age=" + age + "]"; } }
Output:
14318bb;Person [name=Tom, age=20] modify():14318bb;Person [name=Tom, age=20] ca0b6;Person [name=Jack, age=10] modify():ca0b6;Person [name=Jack, age=10] modify():ca0b6;Person [name=Tom-modify, age=10] 14318bb;Person [name=Tom, age=20]
Tips:
hasCode()可以标识对象。Map中判断是否包含一个对象,就是使用hashCode()和Equals()来判断