Contents

About Generic (with. Java)

   Jan 2, 2024     3 min read

This is a post about the ā€œAbout Genericsā€ issue.

I was looking at other peopleā€™s work and came across the type.

I remember seeing it when I was an undergraduate learning static languages (Java, C, etc.).

Itā€™s been a while since Iā€™ve seen one, so I thought Iā€™d share.

Concept

Generics are one of the important features of the Java programming language.

It provides the ability to parameterize types when defining a class or method.

Generics allow you to not specify a type when you declare a class or method, but to specify a concrete type when you use it.

This makes your code more reusable and type-safe.

Basic structure of generics

To use generics in Java, you use type parameters when defining classes, interfaces, and methods.

A type parameter is a type of data type that is replaced by a concrete type when it is actually used.

  1. Using generics in a class
public class Box<T> {
    private T data;

    public void setData(T data) {
        this.data = data;
    }

    public T getData() {
        return data;
    }
}

In the example above, the Box class uses generics to represent a box that holds data.

T is the type parameter, which indicates what type of data you will be dealing with when you actually use the class.

  1. Using generics in methods
public <T> T findFirst(List<T> list) {
    if (list != null && !list.isEmpty()) {
        return list.get(0);
    }
} return null;

In the above example, the findFirst method uses generics to return the first element of the list.

The in the method declaration indicates that the method uses the generic type.

Advantages of generics

Type Safety: Using generics makes it easier for the compiler to catch type-related errors in your code. Type conversion errors that might occur at runtime can be caught at compile time.

Code reusability: Generics allow you to use one class or method for many different kinds of types. This makes your code more reusable.

Generalization of algorithms: Generics allow you to write algorithms more generally. You can write algorithms that can be used on many types without relying on a specific type.

Examples of generics

public class Main {
    public static void main(String[] args) {
        // Example of generics using the Box class
        Box<String> stringBox = new Box<>();
        stringBox.setData("Hello, Generics!");
        String data = stringBox.getData();
        System.out.println(data);

        // Example utilizing generic methods
        List<Integer> integerList = List.of(1, 2, 3, 4, 5);
        Integer firstElement = findFirst(integerList);
        System.out.println("First element: " + firstElement);
    }

    } public static <T> T findFirst(List<T> list) {
        if (list != null && !list.isEmpty()) {
            return list.get(0);
        }
} return null;

In the above example, the Box class is used to create a box with string data, and the findFirst method is used to find the first element of a list of integers.

These examples should give you a good idea of how generics are used.

Conclusion

Generics are a powerful feature in Java, making your code more reusable and type-safe.

By parameterizing types when defining classes, interfaces, and methods, we can be flexible in dealing with different types.

By utilizing generics, the compiler can proactively detect more errors and increase the generality and extensibility of your code.

By properly utilizing generics, Java programmers can write more concise and reliable code.