Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 220 – Processing Spring, 2017

Similar presentations


Presentation on theme: "CSC 220 – Processing Spring, 2017"— Presentation transcript:

1 CSC 220 – Processing Spring, 2017
March / April, 2017 Java Generics in Processing

2 Generic Interfaces / Classes
We have been using generic interfaces and classes from the library. java.util.Set<E>, java.util.HashSet<E> java.util.Map<K,V>, java.util.HashMap<K,V> They allow containers to contain different types (classes) of objects. Generic parameters such as E (element type), K (key type) and V (value type) above are types bound by client code at compile time.

3 Using Generic Classes Application code supplies a binding for each generic parameter that must be an interface or class name. It cannot be a primitive type such as int or float. Java Generic classes are comparable to Template classes in C++. Their main use is to allow a container class such as java.util.HashSet<E> to work with any Java class substituted by your code for class variable E.

4 Building Generics A class or interface specifies its generic type.
HashSet<E>, HashMap<K, V>, etc. ? extends T is a bounded wildcard, T or a subclass ? super T is a lower-bounded wildcard, T or a superclass ? is an unbounded wildcard, same as ? Extends Object The bound constrains client code type binding. Struck-through parts not in CSC220 final. The writer of a generic class must ensure type consistency within the class. The application programmer can safely assume type compatibility if the compiler does not flag an error.

5 Erasure and Restrictions
Unlike C++ template classes, the compiler generates only 1 copy of the class code for a generic class, erasing the generic type T information at run time. Every generic type T is treated as a java.lang.Object at run time. Writers of generic classes may need to cast to generic type T, E, K, V etc. in places. Writers of generic classes must ensure that those casts are valid. The compiler cannot ensure this.

6 Restrictions 1. No constructor – no “new T()” for generic T.
2. No “new T[]” – you must cast an “Object []” to a “T []” within the generic class code. This cast often fails at run time with a wildcard T. Make sure to test if you use arrays of wildcard generic types. 3. No generic T in a static field, static method or static initialization block of code. 4. Exception classes cannot be generic.

7 Non-template class topics
java.lang.Cloneable Marker interface that says (to humans and tools), “Objects of this interface provide a public clone() method. See java.lang.Object.clone() clone() returns a new copy of its object. Its implementation often uses a private constructor to build the new object. An immutable object can return a reference to itself. It is like a C++ copy constructor. Do not clone 2D arrays. An array clone() clones only the first dimension.

8 Non-template class topics
java.util.Comparable<T> Specifies method int compareTo(T o). Returns -1, 0 or 1 if object is logically <, == or > o. May require redefining equals() and hashCode() in class java.lang.Object. Just exclusive-OR (^) the hashCodes of the fields. java.util.Serializable Useful for storing objects in binary data files or sending them across a network. All fields in a Serializable object should be Serializable, primitive types or transient.


Download ppt "CSC 220 – Processing Spring, 2017"

Similar presentations


Ads by Google