1 / 9

COP3804 - Intermediate java

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 .

havyn
Download Presentation

COP3804 - Intermediate java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. COP3804 - Intermediate java Generics

  2. 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.

  3. 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<Person> specifies a version of the generic ArrayList class that can hold Person elements only. ArrayList<String> specifies a version of the generic ArrayList class that can hold String elements only.

  4. 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 Student extends Person implements Comparable<Student>

  5. 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);

  6. 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.

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



  8. 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.

  9. 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

More Related