1

I'm trying to use the charts4j api and one of the constructors has this:

public static Data newData(List<? extends Number> data)

It looks like some form of generics to me, but I've never seen this notation before and I don't understand it.

nullByteMe
  • 6,141
  • 13
  • 62
  • 99
  • 1
    http://docs.oracle.com/javase/tutorial/extra/generics/methods.html – Kon Jul 26 '13 at 17:39
  • @Down voter what did I do wrong? – nullByteMe Jul 26 '13 at 19:55
  • 1
    I downvoted the question because it doesn't show research effort. I googled "java question mark extends" and immediately found prior SO questions as well as the Java Tutorials Generics article on wildcards. – Paul Bellora Jul 26 '13 at 20:09
  • @paulbellora I respect your down vote but please don't jump to conclusions. No where did I say I didn't search, but that I didn't understand. – nullByteMe Jul 26 '13 at 21:51
  • @code4me I believe you but the question doesn't *show* research effort. For the future, make sure to cite and link to resources that you've already read and explain why they haven't helped or what about them you didn't understand. – Paul Bellora Jul 26 '13 at 22:34

6 Answers6

5

This is an upper-bounded wildcard: ? extends Number.

It means that data can be a list of anything that is Number or a subclass, such as List<Number>, List<Integer>, List<Double>, etc.

Generics in Java are not covariant, so a List<Double> is not a List<Number>. Here, a parameter of type List<? extends Number> allows List<Double> as well as List<Number>, but a parameter of type List<Number> does not allow List<Double>.

As for the List part, it could be anything that implements List, such as LinkedList<Integer> or ArrayList<Double>.

rgettman
  • 176,041
  • 30
  • 275
  • 357
1
public static Data newData(List<? extends Number> data)

This defines a method that receives a collection implementing the List interface (ArrayList, for example), that contains any subclass of the Number class (even the Number class itself).

Indeed, this concept is related to generics and it's called an Upper Bounded Wildcard. Long story short: It allows you to write a method without a specific type restriction, but a hierarchy restriction instead.

Fritz
  • 9,987
  • 4
  • 30
  • 49
1

Yes it is wildcard in generics. It means the method will accept any List of type of class that extends Number.

Example: List<Integer>, List<Double>

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
1
<T extends SomeClass>

is used when the actual parameter can be some class or any sub class of it. So in your case:

public static Data newData(List<? extends Number> data)

your method can accept a list of any class that is of type Number.

To learn more about Java generics refer:

http://docs.oracle.com/javase/tutorial/extra/generics/methods.html

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

It is a generic type. It means that the data parameter is a List of any class that extends Number. i.e. If you have a custom class:

public class Nomber extends number {
    //stuff...
}

it will take a List<Nomber> as a variable.

sparks
  • 736
  • 1
  • 9
  • 29
0

It means that the data list can only add a object of Number type or SubNumber type as Double, Integer...

ThiepLV
  • 1,219
  • 3
  • 10
  • 21