From: www on 16 Jul 2010 15:47 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. Thank you very much.
From: www on 16 Jul 2010 15:52 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?
From: Peter Duniho on 16 Jul 2010 15:56 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. No. Setting "p" to null equals setting "p" to null, only. The variable "tom" is unchanged, as is the object to which it refers. Note that the list in your example is unnecessary for the demonstration. Your example is essentially equivalent to this: Person tom1, tom2; tom1 = new Person("Tom"); tom2 = tom1; tom1 = null; And again, assigning null to the variable affects only the variable, not the object to which that variable referred, nor any other variable or other memory location that may also refer to that object (such as "tom2"). Pete
From: Peter Duniho on 16 Jul 2010 15:57 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? Impossible to say without seeing the actual code. But, assuming a conventional implementation of setAge() and getAge(), then after your loop, calling tim.getAge() will return 30. Pete
From: Patricia Shanahan on 16 Jul 2010 15:58 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. Huh? Immediately before you set p to null, p and tom both pointed to the same Person object, but you didn't do anything to that object. Changing p to not point to the object does not change the object, or any other pointers to it. Patricia
|
Next
|
Last
Pages: 1 2 3 4 Prev: what the benefit is by using annotation, like "@Immutable" ? Next: Ordering of hashtable keys |