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

S

Stefan Ram

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>] ")"
 
S

Stefan Ram

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>] ")"
 
C

Chris Smith

Stefan Ram said:
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
 
T

Thomas Hawtin

Stefan said:
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
 
S

Stefan Ram

Chris Smith said:
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.
 
H

Hendrik Maryns

Stefan Ram schreef:
Chris Smith said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top