HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Using Java Generics"
Vol. 8, Issue 11, p. 36

	



Listing 1

1 package example1;
2
3 import java.util.ArrayList;
4
5 public class OldCollection{
6
7 public
8 static void main(String[] a){
9 ArrayList strLst =
10 new ArrayList();
11 strLst.add("hello");
12 strLst.add("goodbye");
13 //...
14 //Object reference
15 String strFromList =
16 (String)strLst.get(1);
17 }
18 }

Listing 2

1 package example1;
2
3 import java.util.ArrayList;
4
5 public class UseGenerics{
6
7 public
8 static void main(String[] a){
9 ArrayList<String> strLst =
10 new ArrayList<String>();
11 strLst.add("hello");
12 strLst.add("goodbye");
13 //...
14 String strFromList =
15 strLst.get(1);
16 }
17 }

Listing 3

1 class GenericBean<VarType>{
2
3 private VarType x;
4
5 public GenericBean(){}
6 public GenericBean(VarType x){
7 this.x = x;
8 }
9
10 public VarType getX(){
11 return x;
12 }
13 public void setX(VarType x){
14 this.x = x;
15 }
16 }

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.