Chetan
Chetan

Reputation: 1537

Java What is difference between generic method and a method object as a parameter?

What are advantage between a generic method and a method just accepts Object? How does it ensures type safety?

For example: What difference does it make when define my interface in either of the form mentioned in below code snippet?

public interface MyInterface {
  public <MT> String myMethod(MT t);
}

OR

public interface MyInterface {
  public String myMethod(Object t);
}

In my opinion Generic methods are advantageous only when we type bound around it.. for example type parameter should of Serializable class like. Otherwise it doesn't make sense.. looking for more insight

public interface MyInterface {
  public <MT extends Serializable> String myMethod(MT t);
}

Upvotes: 8

Views: 3397

Answers (4)

akhil singhal
akhil singhal

Reputation: 111

You are partially correct. Generic method and method as a object look like same in some context but the major difference is how compiler handles both

For the object as params type conversion is done based on the typecasting basically it is being handled runtime but for the generic type compile time only it is being handled.

Compile time handling is much better than the run time one. So the generic method is good to use as compare to object as a parameter in your context

Upvotes: 0

stdout
stdout

Reputation: 2651

It can be also nice to see what it means from the compile/runtime point of view.

If you use generics, then the compiler generates the necessary code during compilation and you would't do runtime casting on your relevant objects and/or type checking.

On the other hand, if you use only the Object class as a generic type, you would probably end up having slightly less code (since the compiler wouldn't be generating anything) but you would need to take care of runtime type safety and casting by yourself.

Upvotes: 0

LPD
LPD

Reputation: 2883

Generic method restricts the type of parameter that can be passed to the method. That brings in more cohesive code which limits the input that it can work on, and thus can be reasonably assumed to have certain features.

For e.g

public interface MyInterface {
    public [MT extends BaseClass] String myMethod(MT t);
}

Here you always know that all the methods that are applicable for BaseClass are applicable to t in this method.

Upvotes: -1

JB Nizet
JB Nizet

Reputation: 691715

A method is usually made generic to ensure that two arguments are compatible with each other, or to return a value whose type depends on the generic type of the method.

For example,

public static <T> void sort(List<T> list, Comparator<? super T> c)

makes sure the type of the list and the type of the comparator are compatible, which wouldn't necessary be the same if the signature was

public static void sort(List list, Comparator c)

In the following example, the returned value's type depends on the generic type of the method:

public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll)

which allows doing:

List<Integer> intList = ...;
Integer min = Collections.min(intList);

If the method was

public static Comparable T min(Collection coll)

you would have to do

Integer min = (Integer) Collections.min(intList);

and you wouldn't have any warning from the compiler if you changed the code to

Long min = (Long) Collections.min(intList);

Upvotes: 18

Related Questions