From: Eric Sosman on 16 Jul 2010 16:02 On 7/16/2010 3:47 PM, www wrote: > > Person tom = new Person("Tom"); > > Person tim = new Person("Tim"); > > List<Person> peopleList = new ArrayList<Person>(); > > peopleList.add(tom); > peopleList.add(tim); > > for(Person p : peopleList) > { > if(p.getName().equalsIgnoreCase("Tom")) > { > p = null; > } > } > > now, I expect the reference tom will be null, but it is not. Why? I > thought the reference p is just an alias of reference tom. Both are > pointing to the same object. Setting p to null equals to setting tom to > null. Write your address on a piece of yellow paper; call that paper `tim'. Write it again on a piece of white paper; call that one `p'. Erase what's written on the white paper. Is the yellow paper now blank? -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Patricia Shanahan on 16 Jul 2010 16:03 www wrote: > > Sorry. Another related question: > > Person tom = new Person("Tom"); > > Person tim = new Person("Tim"); > tim.setAge(40); > > List<Person> peopleList = new ArrayList<Person>(); > > peopleList.add(tom); > peopleList.add(tim); > > for(Person p : peopleList) > { > if(p.getName().equalsIgnoreCase("Tom")) > { > p = null; > } > else if(p.getName().equalsIgnoreCase("Tim")) > { > p.setAge(30); > } > } > > tim.getAge() is 40 or 30? Now you are using p to do something to the Person object it points to. Specifically, when it points to the Person object with getName result "Tim" you call that object's setAge method. Assuming setAge and getAge set and get the same field in the Person object, tim.getAge() will be 30 after the p.setAge(30) call. Patricia
From: Patricia Shanahan on 16 Jul 2010 16:10 Eric Sosman wrote: > On 7/16/2010 3:47 PM, www wrote: >> >> Person tom = new Person("Tom"); >> >> Person tim = new Person("Tim"); >> >> List<Person> peopleList = new ArrayList<Person>(); >> >> peopleList.add(tom); >> peopleList.add(tim); >> >> for(Person p : peopleList) >> { >> if(p.getName().equalsIgnoreCase("Tom")) >> { >> p = null; >> } >> } >> >> now, I expect the reference tom will be null, but it is not. Why? I >> thought the reference p is just an alias of reference tom. Both are >> pointing to the same object. Setting p to null equals to setting tom to >> null. > > Write your address on a piece of yellow paper; call that paper > `tim'. Write it again on a piece of white paper; call that one `p'. > Erase what's written on the white paper. Is the yellow paper now > blank? > Good analogy. It can also be used for the second question. Go chalk "30" on the door of the house at the address on the white paper. Later, go to the address on the yellow paper, and see what number is on the door. Patricia
From: Eric Sosman on 16 Jul 2010 16:12 On 7/16/2010 3:52 PM, www wrote: > > Sorry. Another related question: > > Person tom = new Person("Tom"); > > Person tim = new Person("Tim"); > tim.setAge(40); > > List<Person> peopleList = new ArrayList<Person>(); > > peopleList.add(tom); > peopleList.add(tim); > > for(Person p : peopleList) > { > if(p.getName().equalsIgnoreCase("Tom")) > { > p = null; > } > else if(p.getName().equalsIgnoreCase("Tim")) > { > p.setAge(30); > } > } > > tim.getAge() is 40 or 30? Write your address on a piece of yellow paper; call the paper `tom'. Write your main squeeze's address on a piece of pink paper; call the paper `tim'. With me so far? Yellow, pink, keep 'em straight. Copy what's written on the yellow paper onto a piece of white paper called `p', then erase it. Copy what's on the pink paper to `p', show it to the pizza guy, and say "Deliver a King Kong Killer Kolesterol Kannonball (no anchovies) to this address." Where does the pizza go? (Assume the pizza guy can read and remember ...) -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Mike Schilling on 17 Jul 2010 14:21 Or, to paraphrase everyone else who's answered this question, Java does not have aliasing of references.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: what the benefit is by using annotation, like "@Immutable" ? Next: Ordering of hashtable keys |