"new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>] ")"

Discussion in 'Java' started by Stefan Ram, Dec 3, 2005.

  1. Stefan Ram

    Stefan Ram Guest

    According to

    http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9

    the following production holds

    <ClassInstanceCreationExpression> ::=
    "new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>] ")"

    What would be an example for a class instance creation expression
    with type arguments? Here's a reminder about those:

    <TypeArguments> ::=
    "<" <TypeArgument> {"," <TypeArgument>} ">"

    What I would understand would be:

    <ClassInstanceCreationExpression> ::=
    "new" <ClassOrInterfaceType> [<TypeArguments>] "(" [<ArgumentList>] ")"
    Stefan Ram, Dec 3, 2005
    #1
    1. Advertising

  2. Stefan Ram

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >What I would understand would be:
    ><ClassInstanceCreationExpression> ::=
    >"new" <ClassOrInterfaceType> [<TypeArguments>] "(" [<ArgumentList>] ")"


    OK, this does not make sense, because "<TypeArguments>"
    already are part of "<ClassOrInterfaceType>" -- so what I
    understand would be:

    "new" <ClassOrInterfaceType> "(" [<ArgumentList>] ")"
    Stefan Ram, Dec 3, 2005
    #2
    1. Advertising

  3. Stefan Ram

    Chris Smith Guest

    Stefan Ram <-berlin.de> wrote:
    > -berlin.de (Stefan Ram) writes:
    > >What I would understand would be:
    > ><ClassInstanceCreationExpression> ::=
    > >"new" <ClassOrInterfaceType> [<TypeArguments>] "(" [<ArgumentList>] ")"

    >
    > OK, this does not make sense, because "<TypeArguments>"
    > already are part of "<ClassOrInterfaceType>" -- so what I
    > understand would be:
    >
    > "new" <ClassOrInterfaceType> "(" [<ArgumentList>] ")"


    Does this help?

    public class Test<T>
    {
    public <E> Test()
    {
    }

    public static void main(String[] args)
    {
    Test<String> t = new <Number> Test<String>();
    }
    }

    In the line of code in main, "<Number>" matches the TypeArguments in the
    production for ClassInstanceCreationExpression. <String> matches the
    TypeArguments for ClassOrInterfaceType. The former is a set of explicit
    type arguments on the constructor, not the class.

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Dec 3, 2005
    #3
  4. Re: "new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>]")"

    Stefan Ram wrote:
    > According to
    >
    > http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.9
    >
    > the following production holds
    >
    > <ClassInstanceCreationExpression> ::=
    > "new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>] ")"
    >
    > What would be an example for a class instance creation expression
    > with type arguments? Here's a reminder about those:
    >
    > <TypeArguments> ::=
    > "<" <TypeArgument> {"," <TypeArgument>} ">"
    >
    > What I would understand would be:
    >
    > <ClassInstanceCreationExpression> ::=
    > "new" <ClassOrInterfaceType> [<TypeArguments>] "(" [<ArgumentList>] ")"


    It's the same as the type arguments before a method. I can't quite think
    why you would want it. Potentially you might want two arguments, say,
    Comparable, but in a constructor?

    new <String>MyClass(strListA, strListB)

    public <T> MyClass(List<T> a, List<T> b) { ... }

    Looks unlikely to me. Almost certainly you could use wildcard instead.

    There appears to be a slight error in the stated grammar. javac does not
    accept wildcard types there. They wouldn't make any sense in that position.

    Tom Hawtin
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
    Thomas Hawtin, Dec 3, 2005
    #4
  5. Stefan Ram

    Stefan Ram Guest

    Chris Smith <> writes:
    >public class Test<T>
    >{
    > public <E> Test()
    > {
    > }
    >
    > public static void main(String[] args)
    > {
    > Test<String> t = new <Number> Test<String>();
    > }
    >}
    >Does this help?


    Yes. So, "Number" is the argument for the constructor's type
    parameter "E". I believe that I was able to grasp the
    formalism, though I would still will have to sit down and
    think about it in order to find an example where such a
    parameter is helpful.
    Stefan Ram, Dec 3, 2005
    #5
  6. Re: "new" [<TypeArguments>] <ClassOrInterfaceType> "(" [<ArgumentList>]")"

    Stefan Ram schreef:
    > Chris Smith <> writes:
    >
    >>public class Test<T>
    >>{
    >> public <E> Test()
    >> {
    >> }
    >>
    >> public static void main(String[] args)
    >> {
    >> Test<String> t = new <Number> Test<String>();
    >> }
    >>}
    >>Does this help?

    >
    >
    > Yes. So, "Number" is the argument for the constructor's type
    > parameter "E". I believe that I was able to grasp the
    > formalism, though I would still will have to sit down and
    > think about it in order to find an example where such a
    > parameter is helpful.


    I seem to recall to have seen something like this in the source code of
    HashMap. Anyway, the same can be done when invoking methods against an
    object/a class, and unfortunately you really need it, as in:

    public class SomeClass{
    public void someMethod(Set<String>){}
    }

    public class Main{
    public static void main(String[] args){
    someMethod(Collections.<String>emptySet())
    }
    }

    You really need the <String> there, otherwise the compiler will complain
    method is not applicable to the arguments Set<Object>.

    See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6293352

    HTH, H.

    --
    Hendrik Maryns

    ==================
    www.lieverleven.be
    http://aouw.org
    Hendrik Maryns, Dec 5, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robb Smith
    Replies:
    2
    Views:
    558
    Saravana
    Jul 18, 2003
  2. =?Utf-8?B?UGF0cmljaw==?=

    Open new with new session

    =?Utf-8?B?UGF0cmljaw==?=, Dec 18, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    3,317
    tittlejoseph
    Sep 2, 2007
  3. Gawel
    Replies:
    1
    Views:
    386
    Alvin Bruney
    Dec 31, 2003
  4. =?Utf-8?B?QmVuamFtaW4=?=
    Replies:
    0
    Views:
    301
    =?Utf-8?B?QmVuamFtaW4=?=
    Feb 12, 2004
  5. Replies:
    2
    Views:
    436
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page