0

I was reading Thinking in Java 4th edition, and in the Chapter Generics, I found these sentences: This paragraph is explaining why array support covariance but generics don't.

The real issue is that we are talking about the type of the container, rather than the type that the container is holding. Unlike arrays, generics do not have built-in covariance. This is because arrays are completely defined in the language and can thus have both compile-time and run-time checks built in,but with generics, the compiler and runtime system cannot know what you want to do with your types and what the rules should be.

but I cannot really understand what this paragraph means, why the compiler know what you want to do with arrays but cannot know what you want to do with generics? can anybody give me any example?

1
  • 1
    That quote is basically meaningless. Is there any more context that you could provide? Commented Oct 24, 2016 at 7:15

2 Answers 2

2

First of all, the quote is very unclear. Both arrays and generics are "completely defined in the language", albeit in a different way. And neither the compiler nor the run-time system can read your mind and thus do not know "what you want to do with your types".

The quote seems to refer to the fact that array are reified, while generics are not: at runtime, the element type of a List is not known, but the element type of an array is. That is: at runtime, both a List<String> and a List<Integer> have the same type (a List), whereas a String[] and an Integer[] have different types. At compile time, however, List<String> and List<Integer> are different types.

The reason for this is mostly a historical one. Arrays were introduced in the very first version of Java, and there was no reason not to make the element type of an array known at runtime. When generics were introduced in Java 5, however, the goal was to make new Java code compatible with old Java code (and vice-versa), and therefore generics had to be erased at run-time.

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

Comments

1

To support legacy code, newer versions of Java allow type-safe code to be used with older code. While creating the bytecode, the Java compiler replaces all the type-safe declarations with relevant casts (casting to Object if no type parameters are present). The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods. This is called Type-erasure. (All of this hassle JUST to support legacy code).

You can find a detailed explanation at my blog here.

4 Comments

Type-erasure only affects the runtime. The compiler has full generic type information. So this does not answer " the compiler [...] cannot know what you want to do with your types and what the rules should be" (not that that phrase makes much sense... )
Yeah i read that line, but what i believe is that the compiler do knows about the generic types.
Yes, the quoted sentence (without maybe some more context) does not seem to make much sense. @mindcache should provide some more information.
@mindcache check out the link i shared and let me know if anything is unclear.

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.