Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why/when should generic methods be used?

Tags:

java

oop

generics

Studying Java, I've come across generic methods.

public <T> void foo(T variable) { }

That is, a method which takes a parameter with an undecided type (á la PHP?). I'm however unable to see how this would be a good solution - especially since I've come to fall in love with a strongly typed languages after coming from a loose ones.

Is there any reason to use generic methods? If so, when?

like image 359
Zar Avatar asked Aug 31 '12 14:08

Zar


People also ask

What is a generic method and when should it be used?

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type. If there isn't such a dependency, a generic method should not be used. It is possible to use both generic methods and wildcards in tandem.

Why use generic methods in Java?

Java Generic methods and generic classes enable programmers to specify, with a single method declaration, a set of related methods, or with a single class declaration, a set of related types, respectively. Generics also provide compile-time type safety that allows programmers to catch invalid types at compile time.


2 Answers

Those who are coming from prior to Java 5 background knows that how inconvenient it was to store object in Collection and then cast it back to correct Type before using it. Generics prevents from those. it provides compile time type-safety and ensures that you only insert correct Type in collection and avoids ClassCastException in runtime.

So it provides compile time type-safety and casting. When you want to write complex APIs with complex method signatures it will save you a lot both when writing the API and when using the API and prevents writing lots of code for casting part and catch your errors at compile time. just take a look at java.util.Collection package and see the source code.

As a developer I always want compiler to catch my error at compile time and inform me when I want to compile it then i will fix my errors and at runtime there won't be many errors related to type-safety.

for more info see :

  • http://javarevisited.blogspot.com/2011/09/generics-java-example-tutorial.html
  • http://javarevisited.blogspot.com/2012/06/10-interview-questions-on-java-generics.html
like image 64
Mehdi Avatar answered Sep 28 '22 07:09

Mehdi


Generics, among other things, give you a way to provide a template -- i.e. you want to do the same thing, and the only difference is the type.

For example, look at the List API, you will see the methods

add(E e)

For every list of the same type you declare, the only thing different about the add method is the type of the thing going into the list. This is a prime example of where generics are useful. (Before generics were introduced to Java, you would declare a list, and you could add anything to the list, but you would have to cast the object when you retrieved it)

More specifically, you might want 2 ArrayList instances, one that takes type1 and one that takes type2. The list code for add is going to do the same thing, execute the same code, for each list (since the two lists are both ArrayList instances), right? So the only thing different is what's in the lists.

(As @michael points out, add isn't a true example of a generic method, but there are true generic methods in the API linked, and the concept is the same)

like image 45
hvgotcodes Avatar answered Sep 28 '22 07:09

hvgotcodes