0

I've been reading on Generics lately, and I came across this method:

protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> callable, RunnableScheduledFuture<V> task) {
        return new ExceptionHandlingFutureTask<V>(callable, task);
    }

You see, I understand why there's a <V> after protected. What I don't understand is why there's a <V> again after RunnableScheduledFuture. I took this particular <V> out of the method, compiled it and there was no error. So why then did the writer decide to put it in there in the first place?

0

4 Answers 4

2

Generally speaking, you can always remove generics with "no error", because they're implemented in Java via erasure, so the actual compiled code simply refers to the raw classes anyway. (That said, you'd need to insert some casts to keep the compiler happy).

In light of the latter case, by removing the generic parameter from the future, you've turned it from "a future that will return a V" into "a future that will return something". Without the generic parameter, people will need to cast the result into the type they want, and the compiler won't be able to check the correctness of this for them.

It's just the same as being able to use a raw ArrayList instead of ArrayList<Integer>; both of them "work", but the latter is cleaner, easier to understand, type-checked by the compiler and doesn't require manual casting.

Sign up to request clarification or add additional context in comments.

1 Comment

Aaaaaaah, I see! Thanks a lot. Makes a lot of sense now :)
1

Because you're specifying that you're returning a RunnableScheduledFuture<V>, in which V is the same type of the Callable you put in your input.

If you remove <V>, you will get no error if you call the method in this way

RunnableScheduledFuture<String> rsf = decorateTask(Callable<Integer> callable, RunnableScheduledFuture<Integer> task);

But, if you don't remove the <V>, this will give you a compiler error.

Comments

0

Because he wants avoid cast warning when

ExceptionHandlingFutureTask<V> var = decorateTask(...);

Comments

0

Because RunnableScheduledFuture requires a parameterized type. Even though you can remove parameterized type from your code (and thus becoming a raw-type), the class definition is parameterized.

Removing the parameterized type, will require you to typecast to the appropriate type later.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.