2

I have always use java Generics with existing collections API and know that it provide compile time type safety.

But I am confused with using wildcard or Bounded type parameters in my classes . Could someone clarify what is the pros of using these when compiler type Erasure actually replaces it with base types. Why should not we direct code to base classes

As an example from java tutorial (http://docs.oracle.com/javase/tutorial/java/generics/genMethods.html)

You can write a generic method to draw different shapes:

public static <T extends Shape> void draw(T shape) { /* ... */ }

The Java compiler replaces T with Shape:

public static void draw(Shape shape) { /* ... */ }

So Why we should not write directly to take Shape parameter, what is benefit of using wildcard or Bounded type?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 2
    None, in this particular case. If you were returning, say, a `List`, then it would be different. – Louis Wasserman Mar 13 '14 at 16:45
  • 2
    Try with a `List` and `List` and pass `List` to both the methods. – Rohit Jain Mar 13 '14 at 16:46
  • There are certain things you can do with generic bound types that you can't do with just the base interface; for example, you can specifically return objects of type `T` from methods (otherwise you'd be limited to returning the base interface and casting to subclasses). Also what previous comments said about e.g. `List`. – Jason C Mar 13 '14 at 16:53
  • By the way: "*The Java compiler replaces T with Shape*". It does not. It replaces it with whatever subtype you pass, it just enforces that that type is a subtype of `Shape`. – Jason C Mar 13 '14 at 16:57

2 Answers2

2

In your example there is absolutely no benefit.

Consider the following method signature

public static <T extends Shape> void draw(List<T> shapes) { /* ... */ }

I can do the following

final List<Circle> circles = /* ... */
final List<Square> squares = /* ... */

draw(squares);
draw(circles);

Which I cannot do with

public static void draw(List<Shape> shapes) { /* ... */ }

This especially important with PECS type situations.

Community
  • 1
  • 1
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
0

With generics the code is type safe and your collections are type safe. That means that more problems will be caught at compile time and not run time.

Without generics,

an ArrayList would be an ArrayList of type object and that means that you can put anything in it.

Generics allow you to make sure whatever you put in the ArrayList is of a specific type. For example if you want to create an ArrayList, you can only put in Cars objects and not anything else.

is simply a placeholder for whatever you actually want. If your program only wants to create an ArrayList of cars and never create different types, you could just as easily use the placeholder instead of E throughout your program.

The thing is, it is important to be able to create different types and do different operations on all different types of objects, so your class that creates the ArrayList will usually be E is for 'element' and then in your other classes, you would create the specific type.

FOR YOUR QUESTION>>>> SEE THIS LINK Java: bounded wildcards or bounded type parameter?

Community
  • 1
  • 1
StreamingBits
  • 137
  • 11