90 likes | 247 Views
COP3804 - Intermediate java. Generics. Generics. Generics enable classes, interfaces and methods to use a placeholder for one or more of the data types it works with. The placeholder is a type parameter .
E N D
COP3804 - Intermediate java Generics
Generics • Generics enable classes, interfaces and methods to use a placeholder for one or more of the data types it works with. The placeholder is a type parameter. • If you attempt to use the class or method with an object that is not of an allowable type, an error occurs at compile time.
Generic Classes • Generic classes include type parameters in their definition, which are specified after the class name. • When an instance of a generic class is created, a type argument is explicitly passed to the class’s type parameter within angle brackets. • ArrayListis an example of a generic class. Its definition uses a type parameter for the type of the elements that will be stored. public class ArrayList<E> For example: ArrayList<Doctor> specifies a version of the generic ArrayList class that can hold Doctor elements only. ArrayList<String> specifies a version of the generic ArrayList class that can hold String elements only.
Generic Interfaces • Interfaces can be generic too; Comparableis an example of a generic interface: public interface Comparable<T> T is a type parameter that specifies the type of objects that this object may be compared to. Example: public class Patient extends Person implements Comparable<Patient>
Raw Types • You can create an instance of a generic class without specifying the actual type argument. An object created in this manner is said to be of a raw type. • The Object type is used for unspecified types in raw types. • When using raw types, it is necessary for the programmer to keep track of types used and use casting: • Example: ArrayList list = new ArrayList(); list.add(new Integer(4)); list.add(new Integer(5)); Object obj = list.get(0); Integer obj1 = (Integer)list.get(1);
Generic Methods • Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic class, but the type parameter's scope is limited to the method where it is declared. • When a generic method is called, the compiler determines which type to use from the type of data being passed to the method.
Benefits of using Generics Code that uses generics has many benefits over non-generic code: • Stronger type checks at compile time. A Java compiler applies strong type checking to generic code and issues errors if the code violates type safety. Fixing compile-time errors is easier than fixing runtime errors, which can be difficult to find. • Elimination of casts. The following code snippet without generics requires casting: List list = new ArrayList(); list.add("hello"); String s = (String) list.get(0);
Benefits of Using Generics continued • When re-written to use generics, the code does not require casting: List<String> list = new ArrayList<String>(); list.add("hello"); String s = list.get(0); // no cast • Enabling programmers to implement generic algorithms. By using generics, programmers can implement generic algorithms that work on collections of different types, can be customized, and are type safe and easier to read.
References • Horstmann, Cay. Big Java 4th ed. New York, USA: John Wiley & Sons, Inc., 2010. • Oracle. The Java Tutorials, 2013. Web. 25 Aug. 2013. http://docs.oracle.com/javase/tutorial/index.html • Gaddis, Tony, and Godfrey Muganda. Starting out with Java: from Control Structures through Data Structures 2nd ed. Boston, USA: Addison-Wesley, 2012