DEV Community

loading...

Java Tutorial - 8 Generic

nadirbasalamah
A guy who interested in web development and software engineering
・2 min read

Introduction

Generic type is a special type in Java that allows specific data type as a parameter. The generic type is useful to specify the data type in a class instead of making separated classes for each type. This is the comparison between class creation without using generic type and class creation with generic type.

// without generic type, the class for each type is created
class IntBox {
    private int content;
}

class StringBox {
    private String content;
}
Enter fullscreen mode Exit fullscreen mode
// with generic type with <T> syntax
class Box<T> {
    private T content;
}
Enter fullscreen mode Exit fullscreen mode

Creating Generic Type

This is the basic syntax of using generic type

class_name<T1,T2,....,n>
Enter fullscreen mode Exit fullscreen mode

The generic type is declared by using <T> syntax (the T character can be changed with other character, usually the T character is used to define generic). In this example, the generic type is implemented in a class called Box.

// create a class called Box with generic type called "T"
public class Box<T> {
    // using "T" as generic type
    private T content;

    // constructor
    public Box(T content) {
        this.content = content;
    }

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }
}

Enter fullscreen mode Exit fullscreen mode

Main Class

public class MyApp {
    public static void main(String[] args) {
        // using String as a data type
        Box<String> stringBox = new Box<>("Generic is Cool!");

        // using Integer as a data type
        Box<Integer> intBox = new Box<>(12);

        // print out the content
        System.out.println(stringBox.getContent());
        System.out.println(intBox.getContent());
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

Generic is Cool!
12

Enter fullscreen mode Exit fullscreen mode

Based on the code above, the data type is specified inside the angle bracket (<>). The data type that can be used is a reference data type.

In this example, the multiple generic type is used in class called Pair.

public class Pair<K,V> {
    private K key;
    private V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }

    public void setKey(K key) {
        this.key = key;
    }

    public V getValue() {
        return value;
    }

    public void setValue(V value) {
        this.value = value;
    }
}

Enter fullscreen mode Exit fullscreen mode

Based on the code above, the multiple generic type (<K,V>) is used.

Main Class

public class MyApp {
    public static void main(String[] args) {
        Pair<Integer, String> myPair = new Pair<>(1,"Test");

        // get the key and value
        System.out.println(myPair.getKey());
        System.out.println(myPair.getValue());
    }
}

Enter fullscreen mode Exit fullscreen mode

Output

1
Test

Enter fullscreen mode Exit fullscreen mode

Based on the code above, the required types is define inside angle bracket separated by comma (,).

Sources

  • Learn more about generic type in this link.

I hope this article is helpful for learning the Java programming language. If you have any thoughts or comments you can write in the discussion section below.

Discussion (0)