开发者

Java methods that look like: public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)...what are they?

So, I'm looking through a java library (JScience) after someone here thoughfully pointed me towards it for getting Vectors (mathematical ones, that is) in java.

Unfortunately, I've never seen anything in my life before like:

public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)

as a method you can call in the DenseVector class. What...does that even mean. Is it returning a "<F extends Field<F>>" (and if so, why does Eclipse think it's an input?)

http://jscience.org/api/org/jscience/mathematics/vector/DenseVector.html#valueOf(F...)

It really confuses me. I can't make a new DenseVector() because only the super class has that, and it's protected, and trying to do DenseVector.valueOf() apparently only works i开发者_开发百科f I give it...that...weird thing as an input.

I've seen people having to instantiate methods when trying to instantiate objects (or something like that)...is that like that (or IS it that?)) What is the API trying to get me to do?

I'm kind of confused that I've learned java in school (and used it a bit at work, though we use a lot of differnet stuff besides just java), and never came across anything like this. What's it for? What's it trying to get me to do? Is it new? Old? Obscure?

-Jenny


You should be able to invoke this method to create a vector, like this:

Real r1 = Real.ONE, r2 = Real.valueOf(2D), r3 = Real.ZERO;
DenseVector<Real> v = valueOf(r1, r2, r3);

In this example, the type argument F is Real. Real obeys the constraint "extends Field<F>" because it implements Field<Real>.

For different applications, different fields are likely to be used. For example, security applications might use the ModuloInteger field. It's a little confusing because this is a mathematical field, not a "vector field" like one talks about in physics.

By using type variables, this library helps to make sure you perform all operations within a given field. For example, given v declared as a DenseVector<Real> like above, the compiler will complain if you try to multiply it by a Complex number.


It's a generic return type. See here for a tutorial on Java Generics.


These are called Generic types. They've been added in Java 5 and are similar to C++ templates.

The idea is that you define a collection of items of a particular type rather than something general.

This helps you avoid frequent downcasting. In older Java code, suppose that you knew your vector would contain only X's. Once you retrieved items out of that collection, you would just get Object, and you had to explicitly downcast it.

It is also safer because you can't put Ys into a vector of Xs, and clearer to read for the same reasons.

The story behinds the "extends" in these brackets is that you can define collections of "Xs and all their subtypes" that would still accept subtypes of X but reject Y.


public static <F extends Field<F>> DenseVector<F> valueOf(F... elements)

Lets break this down:

public static

Its a public static method.

<F extends Field<F>>

Its a generic method for any class F where F is an extention of Field

DenseVector<F>

It returns a (generic) DenseVector for F

valueOf(F... elements)

A method named valueOf where parameters are zero or more Fs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜