Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jada Mohammad   on Dec 08 In Java Category.

  
Question Answered By: Wilbur Hall   on Dec 08

Upto java  1.4 you can only hold reference types  in the
Collection/Container classes  so you are stuck with
either using arrays or using the wrapper classes for
the primitives. This has been remedied somewhat in
Java 1.5 with the introduction of generics. The syntax
will be familiar if you know templates in C++.

ie.

Stack<int> col = new Stack<int>();
col.add(2);

note that this is not technically equivalent to it's
C++ counterpart in that it is not compiled to a
different type of Stack rather it is more like an
instruction for the compiler to disallow entries to
the above Stack that are not Integers. Note I said
Integers and not int's because the second line above
is "translated" into col.add(new Integer(2));.

For a more complete description of Generics in java go
to:

http://www.ociweb.com/jnb/jnbJul2003.html

Share: 

 
 
Didn't find what you were looking for? Find more on "templated" class Or get search suggestion and latest updates.


Tagged: