Re: Array difference

From: Dale King (kingd_at_tmicha.net)
Date: 11/18/03


Date: Mon, 17 Nov 2003 22:06:58 -0500


"Adam Jenkins" <adam@remove.thejenkins.me.org> wrote in message
news:3FB53FA6.3060905@remove.thejenkins.me.org...
> On this topic, is there any logical reason why the language doesn't
> allow the array literal syntax everywhere? It seems to me that even
> outside of a declaration statement the compiler knows the type of the
> expression. I.e.
>
> int[] array1 = {1,2,3};
> int[] array2;
> array2 = {1,2,3};
>
> I don't see any theoretical reason why the language couldn't have
> allowed the assignment to array2. The compiler knows that array2 is of
> type int[], so why is this any different than the assignment to array1?

I don't have any problem with its use here.

> Same thing for method parameters:
>
> void someMethod(int[] arg) {}
>
> someMethod({1,2,3});
>
> If the compiler knows that the method arg is type int[], then it knows
> to create a int[], just like in a declaration.

This case however will be taken care of in a manner of speaking in JDK 1.5,
which will have a form of variable length argument lists. All the details
have not been published yet, but they plan on having a syntax like this.

Suppose you have a method that should take some number of strings. Currently
you would specify it as taking a String[] parameter and the caller has to
use the initialization syntax for the String array. But in JDK1.5 you will
be able to say:

    void printStrings( String... strings ) { }

Which declares that the method accepts a variable number of strings. You can
call it using any of these syntaxes:

printStrings( "a" );
printStrings( "a", "b", "c" );
printStrings( new String[] { "a", "b", "c" } );

Internally the strings parameter is just a String array. The compiler will
generate the same code for the last two calls above, but let's you specify
it more cleanly.

--
  Dale King


Relevant Pages

  • Re: Which is better - a char type or a string of length one?
    ... indexing bits from the left or from the right and are you using these ... So strings and integers share the same property that index and slice ... On arrays of type T (although the language uses variants), ... A single-element slice of an array can be achieved with a single index ...
    (comp.lang.misc)
  • Re: c / c++ : is it end of era ?
    ... When you start talking about "bugs in the language", ... Zero terminated strings. ... array information is never destroyed. ...
    (comp.lang.c)
  • Re: easy way to parse
    ... > be slightly tweaked to get around some special feature of the language) ... > info. So, have only one syntax rule for array dereference/function call, ... In the worst case scenario they print the parse tree ...
    (comp.programming)
  • Re: It Hurts When I Do This
    ... We do have a syntax that means the entire array - the array name. ... I disagree that it helps to use a special notation to indicate that ... I think it a mistake to throw all this kind of thing in the language. ...
    (comp.lang.fortran)
  • Re: easy way to parse
    ... be slightly tweaked to get around some special feature of the language) ... info. So, have only one syntax rule for array dereference/function call, ... In the worst case scenario they print the parse tree ...
    (comp.programming)