How to implement mathematical expression in Java?

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

Was this solution helpful to you?

Just Added Q & A:

Find solution

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.