Pages

Sunday 27 January 2013

Why should you use Generics instead of Objects ?

http://stackoverflow.com/questions/5207115/java-generics-t-vs-object

What is the difference between the two ?

public Object doSomething(Object obj) {....}
public T doSomething(T t) {....}

compile time safety that that like works. If the Object version is ued, you won't be sure if the method always returns Foo. If it returns Bar, you'll have a ClassCastException, at runtime.

No comments:

Post a Comment