subanki
subanki

Reputation: 1429

why can't java have types as arguments?

why can't java have types as arguments ?

e.g. can't have Vector of reals where compiler checks type.

(example taken from http://www.ics.uci.edu/~kibler/javacourse/java.html)

Upvotes: 1

Views: 164

Answers (3)

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340341

Use a collection with generics support, such as Vector<Double> instead of its non generic counterpart.

Nowadyas Java does support "types as parameters", or genericity. Read more on that here. Your link is referring to ancient Java versions.

Upvotes: 2

hobbs
hobbs

Reputation: 240049

Because that page is 12 years old and not very well written besides. Java actually does have generics.

Upvotes: 4

bakkal
bakkal

Reputation: 55448

I think that might be outdated, as now you can do:

Vector<Real> vec = new Vector<Real>();

and the compiler will signal an error if you try to :

vec.add(new Animal());

Upvotes: 6

Related Questions