Re: Array difference
From: Dale King (kingd_at_tmicha.net)
Date: 11/18/03
- Next message: Eric Sosman: "Re: Recursion-related problem"
- Previous message: Michael Borgwardt: "Re: Java code analyzer recommendations?"
- In reply to: Adam Jenkins: "Re: Array difference"
- Next in thread: Tor Iver Wilhelmsen: "Re: Array difference"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Eric Sosman: "Re: Recursion-related problem"
- Previous message: Michael Borgwardt: "Re: Java code analyzer recommendations?"
- In reply to: Adam Jenkins: "Re: Array difference"
- Next in thread: Tor Iver Wilhelmsen: "Re: Array difference"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|