Generics, wildcard, java?

It's invalid syntax to instantiate a generic type with wildcards. The type List To create an instance of this type doesn't make sense, because with instantiation you're creating something specific.

Up vote 3 down vote favorite share g+ share fb share tw.

Please explain this generic code wildcard compile time error //no compile time error. List x=new ArrayList(); //compile time error. List x=new ArrayList(); java generics wildcard link|improve this question asked Feb 5 at 5:25Sam Adams3108 90% accept rate.

– Bill Feb 5 at 5:53 1 first example works on java 7 obviously and not on java 6 since the optional diamonds werent allowed in 6. Second example fails in both – Sam Adams Feb 5 at 5:56 1 First one definitely wouldn't work on 6; no diamond operator. As to the rest; you might want to check this resource: angelikalanger.com/GenericsFAQ/JavaGener... – Bill Feb 5 at 6:00 ya ya thats right I just edited the comment thanks for this link.

– Sam Adams Feb 5 at 6:01.

It's invalid syntax to instantiate a generic type with wildcards. The type List means a List of some type that is or extends Number. To create an instance of this type doesn't make sense, because with instantiation you're creating something specific: new ArrayList();//compiler:"Wait, what am I creating exactly?

" Generic types with wildcards only make sense for variables and method parameters, because this allows greater freedom in what can be assigned/passed into them. //compiler:"Okay, so passing in a List or a List are both fine" public void eatSomeNumbers(List numbers) { for (Number number : numbers) { System.out. Println("om nom " + number + " nom"); } } Make sure to keep in mind the limitations that come with using wildcards.

List numList = ... numList. Add(new Integer(3));//compiler:"Nope, cause that might be a List" As for your first example, the diamond is a new feature in Java 7 that allows the compiler to infer the type of the new generic instance, based on the type of the variable it's assigned to. In this case: List x = new ArrayList(); The compiler is most likely inferring new ArrayList() here, but what's inferred hardly matters, as long as it's a valid assignment to the given variable.

This was the reason for the diamond operator being introduced - that specifying the generic type of a new object was redundant, as long some generic type would make it a valid assignment/argument. This reasoning only makes sense if you remember that generics in Java are a purely compile-time language feature, because of type erasure, and have no meaning at runtime. Wildcards exist only because of this limitation.

By contrast, in C# generic type information sticks around at runtime - and generic wildcards don't exist in that language.

1: Very detailed explanation – Mark Bouchard Feb 6 at 21:34.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions