12 October 2021

The Basics of Generic Classes

The class ArrayList<T> The T is the type parameter. Classes using a type parameter are called generic classes.

The type parameter represents the type of item that is present in the array list. Lster, we will write a very simple generic classe Pair<S, T>.

Here is how you make an instance of the generic array list classs.


jshell> ArrayList<String> al = new ArrayList<>();
al ==> []

Now let's add some entries.


jshell> al.add("hello");
$2 ==> true

jshell> al.add("goodbye");
$3 ==> true

Primitives not allowed! Primitive types cannot be used for type parameters. However, every primitive type has a wrapper type. We digress briefly to discuss them.

WrapperIntegerByteShortLongFloatDoubleBooleanCharacter
Primitive
int
byte
short
long
float
double
boolean
char

jshell> Integer i = 3;
i ==> 3

jshell> i.getClass()
$5 ==> class java.lang.Integer

jshell> i++;
$6 ==> 3

jshell> i
i ==> 4

jshell> //autoboxing:  Integer i = 3;

jshell> int t = i;
t ==> 4

jshell> //autounboxing

jshell> new Integer(5)
$9 ==> 5

jshell> //calls to new in the wrapper types are tacit.

jshell> Integer.max(3,4)
$10 ==> 4

jshell> Integer.max(3,4,5)
|  Error:
|  method max in class java.lang.Integer cannot be applied to given types;
|    required: int,int
|    found:    int,int,int
|    reason: actual and formal argument lists differ in length
|  Integer.max(3,4,5)
|  ^---------^

jshell> Integer.toString(42)
$11 ==> "42"

jshell> Integer.parseInt("323")
$12 ==> 323

jshell> Integer.parseInt("101110111", 2)
$13 ==> 375

jshell> Integer.parseInt("101110111", 3)
$14 ==> 7627

jshell> Integer.parseInt("101110111", 16)
|  Exception java.lang.NumberFormatException: For input string: "101110111" under radix 16
|        at NumberFormatException.forInputString (NumberFormatException.java:68)
|        at Integer.parseInt (Integer.java:652)
|        at (#15:1)

jshell> Integer.parseInt("10111", 16)
$16 ==> 65809

jshell> new ArrayList<String>()
$17 ==> []

jshell> StringBuffer sb = new StringBuffer();
sb ==> 

jshell> sb.append("cows")
$19 ==> cows

jshell> sb.append("horses")
$20 ==> cowshorses

jshell> sb.append(42)
$21 ==> cowshorses42

jshell> sb.toString()
$22 ==> "cowshorses42"

jshell> //how do I clear a string buffer?

jshell> sb.delete(0, sb.length())
$23 ==> 

jshell> sb
sb ==> 

jshell> for(char k = 'a'; k <= 'z'; k++){sb.append(k);}

jshell> sb
sb ==> abcdefghijklmnopqrstuvwxyz

jshell> 

public class Pair<S, T>
{
    private S s;
    private T t;
    public Pair(S s, T t)
    {
        this.s = s;
        this.t = t;
    }
    public S getS()
    {
        return s;
    }
    public T getT()
    {
        return t;
    }
    @Override
    public String toString()
    {
        return String.format("(%s, %s)", s.toString(), t.toString());
    }
    public static void main(String[] args)
    {
        Pair<String, String> quack = new Pair<>("Duck", "l'orange");
        System.out.println(quack);
        //use wrapper types for primitive type
        //parameters.
        Pair<String, Integer> ccEntry = new Pair<>("syzygy", 3);
        System.out.println(ccEntry);
    }
}