简体   繁体   中英

why can't java have types as arguments?

why can't java have types as arguments ?

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

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

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());

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM