I was wondering what is the difference between the following two method declarations:
public Object doSomething(Object obj) {....}
public <T> T doSomething(T t) {....}
Is there something you can/would do with one but not the other? I could not find this question elsewhere on this site.
Isolated from context – no difference. On both
tandobjyou can invoke only the methods ofObject.But with context – if you have a generic class:
Then:
Same code with object
Two advantages:
Objectversion is used, you won’t be sure that the method always returnsFoo. If it returnsBar, you’ll have aClassCastException, at runtime.