Java Generics: A simple example

Sometimes you will see funny angle brackets in Java code. Although Java’s generic types are not mentioned in the IB syllabus, this is a brief example of their use.

The output of this code is:

Hello
5
java.util.Random@7852e922
Anything object: Fred, 5
Anything object: Second, 2

package genericsdemo;
import java.util.List;
import java.util.ArrayList;
/**
 *
 * @author justin
 */
public class GenericsDemo {

    public static void main(String[] args) {
        /* Here we create four MyContainer objects and show that we can put
         * any type into them. Note that the generic type has to be a reference
         * type; we couldn't put a primitive in there.
         */
        MyContainer<String> container1 = new MyContainer("Hello");
        MyContainer<Integer> container2 = new MyContainer(5);
        MyContainer<java.util.Random> container3 = new MyContainer(new java.util.Random());
        MyContainer<Anything> container4 = new MyContainer(new Anything("Fred", 5));
        
        System.out.println(container1.get());
        System.out.println(container2.get());
        System.out.println(container3.get());
        System.out.println(container4.get());
        
        /* It is probably rare that you will use Java Generics to create
         * your own containers. Much more likely you will use it to specify  
         * the type of object to be held in a list object from the Java
         * library.
         */
        List<Anything> myList = new ArrayList();
        myList.add(new Anything("First", 1));
        myList.add(new Anything("Second", 2));
        myList.add(new Anything("Third", 3));
        System.out.println(myList.get(1));
    }
    
}

class MyContainer<T>{
    /* T is not a type, it is a type variable.
     * The variable t is an object of type T, even though we don't yet know 
     * what type T is. 
     */
    T t;
    public MyContainer(T t) {
        this.t = t;
    }
    
    T get(){
        return t;
    }
}

class Anything {
    String name;
    int number;
    
    public Anything(String name, int number) {
        this.name = name;
        this.number = number;
    }
    
    public String toString() {
        return "Anything object: " + name + ", " + number;
        
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s