From: ClassCastException on
On Sat, 17 Jul 2010 11:21:26 -0700, Mike Schilling wrote:

> Or, to paraphrase everyone else who's answered this question, Java does
> not have aliasing of references.

Not quite. It comes close, though, with the right constructs: reference a
points to object 0, which has an instance field b that points to object
1. a.b = null changes the latter. In a sense, reference b is aliased if
we have a reference c that also points to object 0 so c.b == null now as
well.
From: Mike Schilling on


"ClassCastException" <zjkg3d9gj56(a)gmail.invalid> wrote in message
news:i1tto7$f8o$2(a)news.eternal-september.org...
> On Sat, 17 Jul 2010 11:21:26 -0700, Mike Schilling wrote:
>
>> Or, to paraphrase everyone else who's answered this question, Java does
>> not have aliasing of references.
>
> Not quite. It comes close, though, with the right constructs: reference a
> points to object 0, which has an instance field b that points to object
> 1. a.b = null changes the latter. In a sense, reference b is aliased if
> we have a reference c that also points to object 0 so c.b == null now as
> well.

My point was that there's no Java construct that's a reference to a
reference [1], but you're quite right that there are references to objects
that contain references.

1. That is, nothing like the C++

char *p;
char * &q = p;

From: Screamin Lord Byron on
On 07/16/2010 09: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.
>
> Thank you very much.

Others have answered (correctly of course), but here's yet another way
of saying it.

The "for each" loop
for (Person p : peopleList) {}

In this version of the loop, you don't have any means of changing the
contents of a list (you can mutate objects "contained" in the list, but
not the list itself), other than calling peopleList.set() (or remove(),
etc.) methods. For that you also need an index.

The "for each" loop (i believe it was introduced in Java SE 5.0) is
nothing but a shortcut of saying something like:

for (ListIterator<Person> i = peopleList.listIterator();
i.hasNext(); ) {

Person p = i.next();
}

The p object will be exactly the same as in the previous "for each"
loop. In this version of the loop I believe it's much clearer to see why
the list won't change when you set p to null. However, here you have
some extras -- an instance of the ListIterator class. You can use it to
access the list, and you don't even need that p variable. It's quite
useless if you just want to set some list member to null.

So you would write something like:

boolean isEqualToTom = false;
for (ListIterator<Person> i = peopleList.listIterator();
i.hasNext();
isEqualToTom = i.next().getName().equalsIgnoreCase("Tom")) {

if (isEqualToTom) {
i.set(null);
}
}

Of course, this usually is not how you want to do it in real life (it's
quite messy). It's just an illustration of the list iterator. For Lists
you can make your own simple index iterator and just use "for each" loop
with set(int,Object) method of the List interface.




From: ClassCastException on
On Sat, 17 Jul 2010 23:35:39 -0700, Mike Schilling wrote:

> "ClassCastException" <zjkg3d9gj56(a)gmail.invalid> wrote in message
> news:i1tto7$f8o$2(a)news.eternal-september.org...
>> On Sat, 17 Jul 2010 11:21:26 -0700, Mike Schilling wrote:
>>
>>> Or, to paraphrase everyone else who's answered this question, Java
>>> does not have aliasing of references.
>>
>> Not quite. It comes close, though, with the right constructs: reference
>> a points to object 0, which has an instance field b that points to
>> object 1. a.b = null changes the latter. In a sense, reference b is
>> aliased if we have a reference c that also points to object 0 so c.b ==
>> null now as well.
>
> My point was that there's no Java construct that's a reference to a
> reference [1], but you're quite right that there are references to
> objects that contain references.
>
> 1. That is, nothing like the C++
>
> char *p;
> char * &q = p;

That is to say, a reference in and of itself can't be treated as a first-
class object. You can explicitly encapsulate it in one (and the
java.lang.reference classes do so, with almost-immutable ones that the GC
can set to null).

Similarly to how Java lacks first-class functions but allows a one-method
interface like Comparator or ActionListener whose implementations'
instances can be passed around for similar purposes.
From: Mike Schilling on


"ClassCastException" <zjkg3d9gj56(a)gmail.invalid> wrote in message
news:i20me2$hm5$1(a)news.eternal-september.org...
> On Sat, 17 Jul 2010 23:35:39 -0700, Mike Schilling wrote:
>
>> "ClassCastException" <zjkg3d9gj56(a)gmail.invalid> wrote in message
>> news:i1tto7$f8o$2(a)news.eternal-september.org...
>>> On Sat, 17 Jul 2010 11:21:26 -0700, Mike Schilling wrote:
>>>
>>>> Or, to paraphrase everyone else who's answered this question, Java
>>>> does not have aliasing of references.
>>>
>>> Not quite. It comes close, though, with the right constructs: reference
>>> a points to object 0, which has an instance field b that points to
>>> object 1. a.b = null changes the latter. In a sense, reference b is
>>> aliased if we have a reference c that also points to object 0 so c.b ==
>>> null now as well.
>>
>> My point was that there's no Java construct that's a reference to a
>> reference [1], but you're quite right that there are references to
>> objects that contain references.
>>
>> 1. That is, nothing like the C++
>>
>> char *p;
>> char * &q = p;
>
> That is to say, a reference in and of itself can't be treated as a first-
> class object. You can explicitly encapsulate it in one (and the
> java.lang.reference classes do so, with almost-immutable ones that the GC
> can set to null).
>
> Similarly to how Java lacks first-class functions but allows a one-method
> interface like Comparator or ActionListener whose implementations'
> instances can be passed around for similar purposes.

Yup. Java has one hammer and thus everything is a nail.