[880] in java-interest
Re: "new" revisited
daemon@ATHENA.MIT.EDU (Scott Hudson)
Sat Aug 12 20:01:17 1995
From: hudson@cc.gatech.edu (Scott Hudson)
To: java-interest@java.sun.com
Date: Sat, 12 Aug 1995 16:28:13 -0400 (EDT)
In email (reposted by permission) Jonathan Payne (jpayne@starwave.com) says:
>
> I like it the way it is now. But I didn't understand your mail. Can
> you answer some questions for me?
>
> In Java if you say
>
> new Object[5];
>
> That's an array for pointers to Object.
>
> If you say
>
> new Object[5][5];
>
> That's an array for pointers to arrays for pointers to Object.
>
> If you say
>
> new Object[5][5][5];
>
> That's an array for pointers to arrays for pointers to arrays for
> pointers to Object.
>
> Have I got that right?
Absolutely right.
>
> Could you tell me what
>
> (new Object[5])[5][5];
This is always semantically an error. However syntactically, this is
an array of (as you say pointers to) objects, from which we have selected
the 5th object (in this case returning null), which we then treat as an
array and attempt to select the 5th object from again (an error).
Recall in the original discussion "new foo().bar" was declared to be legal
and to mean "(new foo()).bar". Here we are applying a subscript rather than
selecting a member. In general the first "[]" has to be part of the new
since you can't say "new Object" (only "new Object()" or "new Object[]").
> (new Object[5][5])[5];
This is an array of arrays of objects, from which we choose the 5th element
(which in this case is an array of objects).
> (new Object[5][5][5]);
This is an array of arrays of arrays of objects.
>
> mean? Are they all different? Do they all make sense?
>
Yes, they are all very different, and yes they all make sense (although
the first one will always produce an error).
The problem I was getting at was that setting the precedence of "new" to be
higher than "." (per discussions on the list and behavior of javac) gives
us a problem here. "." and "[]" have equal precedence (per the spec), which
implies "new" has higher precedence, than "[]", which in turn argues that just
as "new foo().bar" associates as "(new foo()).bar", so "new Object[5][5][5]"
must associate as "(new Object[5])[5][5]" which is certainly wrong.
Something's got to give. I think it should the declared precedence of "new"
(putting it back where it is in C++ fixes this). The other way to fix it is
to do it by syntactic fiat and say that "new Object[5][5]" means
"(new Object[5][5])" even if "new foo().bar" means "(new foo()).bar". This is
basically what happened in the ad hoc javac compiler. (I haven't looked at the
source, but I'll bet there's a while loop that silently sweeps this little
problem under the rug.
Scott
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com