When and why should I use Class <T> templates classes in Java?

Asked

Viewed 1,468 times

20

When I see a class she gets it, I find it scary.

  • That is Generics. There are interesting applications, but I never really stopped to think about which cases is recommended.

  • Uhum class Generics...

  • Yes. They are not templates. Template is a related thing that exists in C++ (but quite different)

  • 1

    Until someone answers... Using Generics in java

  • Ops thought it was the same.

  • I think it might shed some light: Differences between <T> and <?>

Show 1 more comment

2 answers

18


In C++ this can be called template, but in Java these classes are called generic.

This is necessary when a class can use the same basic structure and algorithms with several data types. Think of something that can be done just like Boolean, Integer, Long, Float, etc. Everything is the same, only changes the type of data that will be used to store in the class. How do you solve this? Make a class for each type of these, copying them and just changing the type?

When you have to give maintenance will remember to do in all the same? No conditions, right?

Then you can use that Generics. With this can vary the type of basic data that will be used in the class according to the form it is instantiated. Then this T there is a kind of "super-variable", which will be replaced by a specific type when the class is instantiated.

class Exemplo<T> {
    private T x;
    Exemplo(T x) => this.x = x;
    public T getValue() => x;
}

Then use:

Exemplo<String> teste1 = new Exemplo<String>("teste");
teste1.getValue();
Exemplo<Boolean> teste2 = new Exemplo<Boolean>(true);
teste2.getValue();

I put in the Github for future reference.

Everything works. In the first example, everywhere we had the T, will turn String. And the second I had T flipped Boolean, would look something like this:

class Exemplo<String> {
    private String x;
    Exemplo(String x) {
        this.x = x;
    }
    public String getValue() {
        return x;
    }
}

Thus a single class solves the problem for all types without having to copy code, which will cause maintenance problems.

Java is full of these classes. Data collections often benefit greatly from this. At first Java didn’t have this feature, so you had a ArrayList "naked" like that. So he would accept anything. It even works, but imagine that you can mix numbers, with Strings, with Clientes (a class you defined), with anything else. When you created that ArrayList<T>, you create a variable that stores ArrayList<Cliente> and this list can only be stored Clientes. It is safer. Basically this is a collection that can be used with any type already existing in Java or created by programmers.

Simply put. There are ways to use more advanced, make restrictions of what types can be used in T, have other slots to generalize other parts with a different type, anyway... ask as you are using and new questions arise.

Documentation (continue following the tutorial pages)

  • ArrayList is an example of this?

  • 1

    Yeah, sure, I’ll get better.

  • Very good answer! I’ve always looked for a good way to explain this.

  • Got it! Now it does make sense to me and the way you explain it all gets too easy, bigown.

0

Generics are also very useful in methods in this example :

    public static <T> List<T> asList(T... objects) {
       return ...
    }

you will receive a T-type list based on your parameters;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.