Difference between a deprecated and a legacy API?

I was studying the legacy API's in the Java's Collection Framework and I learnt that classes such as Vector and HashTable have been superseded by ArrayList and HashMap .

However still they are NOT deprecated, and deemed as legacy when essentially, deprecation is applied to software features that are superseded and should be avoided, so, I am not sure when is a API deemed legacy and when it is deprecated.


From the official Sun glossary:

deprecation : Refers to a class, interface, constructor, method or field that is no longer recommended, and may cease to exist in a future version.

From the how-and-when to deprecate guide:

You may have heard the term, "self-deprecating humor," or humor that minimizes the speaker's importance. A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that you should no longer use it, since it has been superseded and may cease to exist in the future.

The @Deprecated annotation went a step further and warn of danger:

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.

References

  • java.sun.com Glossary
  • Language guide/How and When to Deprecate APIs
  • Annotation Type Deprecated API

  • Note that the official glossary does not define what "legacy" means. In all likelihood, it may be a term that Josh Bloch used without an exact definition. The implication, though, is always that a legacy class should never be used in new code, and better replacement exists.

    Perhaps an old code using legacy but non-deprecated class requires no action, since for now at least, they aren't in danger of ceasing to exist in future version.

    In contrast, deprecation explicitly warns that they may cease to exist, so action should be taken to migrate to the replacement.


    Quotes from Effective Java 2nd Edition

    For comparison on how these terms are used in context, these are quotes from the book where the word "deprecated" appears:

    Item 7: Avoid finalizers : The only methods that claim to guarantee finalization are System.runFinalizersOnExit and its evil twin Runtime.runFinalizersOnExit . These methods are fatally flawed and have been deprecated.

    Item 66: Synchronize access to shared mutable data : The libraries provide the Thread.stop method, but this method was deprecated long ago because it's inherently unsafe -- its use can result in data corruption.

    Item 70: Document thread safety : The System.runFinalizersOnExit method is thread-hostile and has been deprecated.

    Item 73: Avoid thread groups : They allow you to apply certain Thread primitives to a bunch of threads at once. Several of these primitives have been deprecated, and the remainder are infrequently used. [...] thread groups are obsolete.

    By contrast, these are the quotes where the word "legacy" appears:

    Item 23: Don't use raw types in new code : They are provided for compatibility and interoperability with legacy code that predates the introduction of generics.

    Item 25: Prefer lists to arrays : Erasure is what allows generic types to interoperate freely with legacy code that does not use generics.

    Item 29: Consider typesafe heterogeneous containers : These wrappers are useful for tracking down who adds an incorrectly typed element to a collection in an application that mixes generic and legacy code.

    Item 54: Use native methods judiciously : They provide access to libraries of legacy code, which could in turn provide access to legacy data. [...] It is also legitimate to use native methods to access legacy code. [...] If you must use native methods to access low-level resources or legacy libraries, use as little native code as possible and test it thoroughly.

    Item 69: Prefer concurrency utilities to wait and notify : While you should always use the concurrency utilities in preference to wait and notify , you might have to maintain legacy code that uses wait and notify .

    These quotes were not carefully selected: they're ALL instances where the word "deprecated" and "legacy" appear in the book. Bloch's message is clear here:

  • Deprecated methods, eg Thread.stop , are dangerous, and should never be used at all.
  • On the other hand, eg wait/notify can stay in legacy code, but should not be used in new code.

  • My own subjective opinion

    My interpretation is that deprecating something is admitting that it is a mistake, and was never good to begin with. On the other hand, classifying that something is a legacy is admitting that it was good enough in the past, but it has served its purpose and is no longer good enough for the present and the future.


    A common interpretation is that Deprecated means that it will be removed in the near future, and Legacy means that it will remain for backwards compatibility or other reasons.

    Both mean that they shouldn't be used by new code.

    In the case of the JDK even Deprecated code will remain, since backwards compatibility is very important for the Java JDK.


    弃用通常意味着有意在未来的某个时候删除功能,而遗留仅仅意味着如果可能的话不应该在新代码中使用它(尽管可能需要互操作的原因)。

    链接地址: http://www.djcxy.com/p/13274.html

    上一篇: 在Java中重写equals和hashCode时应考虑哪些问题?

    下一篇: 已弃用和旧版API之间的区别?