Why didn't Java implement the generic typed collection classes as decorator classes on the original untyped collection classes?
-
To elaborate further: the decorators would store the type parameters as fields, and wrap all calls to the untyped collection classes with type checks. This solution does a better job of achieving Java's stated goal of type-safety for generics (and still maintain bytecode compatibility). For example, it would cause a ClassCastException to be thrown at line 3 (which is the source of the problem) rather than line 4 (which is what Java does today): List<String> ls = new ArrayList<String>(); List lo = ls; lo.add(10); String s = ls.get(0); Another benefit of this solution is that the type parameters can be queried for at runtime. What are the reasons the Java language designers didn't choose this solution at the time they implemented generic typed collection classes?
-
Answer:
Because generics didn't exist in the Java language until version 5. It would have been nice if they'd have been built into the language from day one, since they've had to make various accommodations to incorporate them into the language without breaking too much existing code. But they left them out, because if they'd waited until they had everything they wanted, they'd never have released it. The language is undoubtedly better for them, though they tend to make it verbose, and it's best written with a refactoring editor that helps do some of the work. (Another thing that didn't exist at the time.) Even now they're still working on it, adding more type inferencing in Java 7 and 8.
Joshua Engel at Quora Visit the source
Related Q & A:
- Why Isn't Montgomery Modular Exponentiation Considered For Use In Quantum Factoring?Best solution by cstheory.stackexchange.com
- Why won't my app run in the IOS simulator?Best solution by Stack Overflow
- Why's everyone on my contact list receiving a link(a virus)that says I sent the message to them but I didn't?Best solution by Yahoo! Answers
- Why can't my computer can't detect my external hard drive?Best solution by Yahoo! Answers
- Why didn't CR7 play in the world cup qualifiers against Malta?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.