在 Java 中使用 Deprecated 方法或类是错误的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2941900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it wrong to use Deprecated methods or classes in Java?
提问by Umesh Aawte
I am using eclipse to develop a web application. Just today I have updated my struts version by changing the JAR file. I am getting warnings at some places that methods are deprecated, but the code is working fine.
我正在使用 eclipse 开发一个 web 应用程序。就在今天,我通过更改 JAR 文件更新了我的 struts 版本。我在某些地方收到警告说方法已被弃用,但代码运行良好。
I want to know some things
我想知道一些事情
Is it wrong to use Deprecated methods or classes in Java?
What if I don't change any method and run my application with warnings that I have, will it create any performance issue.
在 Java 中使用 Deprecated 方法或类是错误的吗?
如果我不更改任何方法并在出现警告的情况下运行我的应用程序,会不会产生任何性能问题。
采纳答案by aioobe
1. Is it wrong to use Deprecated methods or classes in Java?
1. 在 Java 中使用 Deprecated 方法或类是错误的吗?
From the definition of deprecated:
A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
带有@Deprecated 注释的程序元素是不鼓励程序员使用的,通常是因为它很危险,或者因为存在更好的替代方案。
The method is kept in the API for backward compatibility for an unspecified period of time, and may in future releases be removed. That is, no, it's not wrong, but there is a better way of doing it, which is more robust against API changes.
该方法保留在 API 中以在未指定的时间内向后兼容,并且可能在未来的版本中被删除。也就是说,不,它没有错,但有一种更好的方法,它对 API 更改更健壮。
2. What if I don't change any method and run my application with warnings that I have, will it create any performance issue.
2. 如果我不更改任何方法并在出现警告的情况下运行我的应用程序,会不会产生任何性能问题。
Most likely no. It will continue to work as before the deprecation. The contract of the API method will not change. If some internal data structure changes in favor of a new, better method, there could be a performance impact, but it's quite unlikely.
很可能没有。它将继续像弃用之前一样工作。API 方法的契约不会改变。如果某些内部数据结构发生变化以支持新的、更好的方法,则可能会对性能产生影响,但这不太可能。
The funniest deprecationin the Java API, is imo, the FontMetrics.getMaxDecent
. Reason for deprecation: Spelling error.
在最有趣的弃用Java API中,是海事组织,FontMetrics.getMaxDecent
。弃用原因:拼写错误。
Deprecated. As of JDK version 1.1.1, replaced by getMaxDescent().
已弃用。从 JDK 版本 1.1.1 开始,由 getMaxDescent() 取代。
回答by Bozhidar Batsov
It's not wrong, it's just not recommended. It generally means that at this point there is a better way of doing things and you'd do good if you use the new improved way. Some deprecated stuff are really dangerous and should be avoided altogether. The new way can yield better performance than the deprecated one, but it's not always the case.
这没有错,只是不推荐。这通常意味着此时有更好的做事方式,如果您使用新的改进方式,您会做得很好。一些不推荐使用的东西真的很危险,应该完全避免。新方法可以比已弃用的方法产生更好的性能,但情况并非总是如此。
回答by Michael Mrozek
It certainly doesn't create a performance issue -- deprecatedmeans in the future it's likely that function won't be part of the library anymore, so you should avoid using it in new code and change your old code to stop using it, so you don't run into problems one day when you upgrade struts and find that function is no longer present
它当然不会造成性能问题 -弃用意味着将来该函数可能不再是库的一部分,因此您应该避免在新代码中使用它并更改旧代码以停止使用它,所以有一天当您升级 struts 并发现该功能不再存在时,您不会遇到问题
回答by abyx
You can still use deprecated code without performance being changed, but the whole point of deprecating a method/class is to let users know there's now a better way of using it, and that in a future release the deprecated code is likely to be removed.
您仍然可以在不改变性能的情况下使用弃用的代码,但弃用方法/类的全部意义在于让用户知道现在有更好的使用方法,并且在未来的版本中可能会删除弃用的代码。
回答by Petar Minchev
It is not wrong, but some of the deprecated methods are removed in the future versions of the software, so you will possibly end up with not working code.
这并没有错,但是在软件的未来版本中删除了一些不推荐使用的方法,因此您最终可能会得到无法运行的代码。
回答by Esko
- Generally no, it's not absolutely wrong to use
deprecated
methods as long as you have a good contingency plan to avoid any problems if/when those methods disappear from the library you're using. With Java API itself this never happens but with just about anything else it means that it's going to be removed. If you specifically plan not to upgrade (although you most likely should in the long run) your software's supporting libraries then there's no problem in usingdeprecated
methods. - No.
- 通常不,
deprecated
只要您有一个很好的应急计划,如果/当这些方法从您正在使用的库中消失时,可以避免任何问题,使用方法并不是绝对错误的。对于 Java API 本身,这永远不会发生,但对于其他任何事情,这意味着它将被删除。如果您特别计划不升级(尽管从长远来看您很可能应该升级)您的软件的支持库,那么使用deprecated
方法就没有问题。 - 不。
回答by polygenelubricants
Terminology
术语
From the official Sun glossary:
来自 Sun 官方词汇表:
deprecation: Refers to a class, interface, constructor, method or field that is no longer recommended, and may cease to exist in a future version.
deprecation:指不再推荐的类、接口、构造函数、方法或字段,并且可能在未来版本中不再存在。
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:
该@Deprecated
注释进一步进了一步和危险警告:
A program element annotated
@Deprecated
is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
注释的程序元素
@Deprecated
是程序员不鼓励使用的元素,通常是因为它很危险,或者因为存在更好的替代方案。
References
参考
Right or wrong?
对还是错?
The question of whether it's right or wrong to use deprecated methods will have to be examined on individual basis. Here are ALLthe quotes where the word "deprecated"appears in Effective Java 2nd Edition:
使用已弃用的方法是对还是错的问题必须根据个人情况进行检查。以下是Effective Java 2nd Edition 中出现“已弃用”一词的所有引号:
Item 7: Avoid finalizers: The only methods that claim to guarantee finalization are
System.runFinalizersOnExit
and its evil twinRuntime.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.
第 7 条:避免终结器:声称可以保证终结的唯一方法是
System.runFinalizersOnExit
和它的邪恶孪生Runtime.runFinalizersOnExit
。这些方法存在致命缺陷,已被弃用。第 66 条:同步访问共享可变数据:库提供了该
Thread.stop
方法,但该方法很久以前就被弃用了,因为它本质上是不安全的——它的使用会导致数据损坏。第 70 项:文档线程安全:该
System.runFinalizersOnExit
方法是线程敌对的,已被弃用。第 73 条:避免线程组:它们允许您一次将某些
Thread
原语应用于一堆线程。其中一些原语已被弃用,其余的很少使用。[...] 线程组已过时。
So at least with all of the above methods, it's clearly wrong to use them, at least according to Josh Bloch.
因此,至少对于上述所有方法,使用它们显然是错误的,至少根据 Josh Bloch 的说法。
With other methods, you'd have to consider the issues individually, and understand WHYthey were deprecated, but generally speaking, when the decision to deprecate is justified, it will tend to lean toward wrong than right to continue using them.
使用其他方法,您必须单独考虑问题,并了解为什么它们被弃用,但一般来说,当弃用决定是合理的时,继续使用它们将倾向于错误而不是正确。
Related questions
相关问题
回答by Peter Tillemans
Aside from all the excellent responses above I found there is another reason to remove deprecated API calls.
除了上面所有出色的响应之外,我发现还有另一个原因要删除已弃用的 API 调用。
Be researching why a call is deprecated I often find myself learning interesting things about the Java/the API/the Framework. There is often a good reason why a method is being deprecated and understanding these reasons leads to deeper insights.
研究为什么不推荐使用调用我经常发现自己在学习有关 Java/API/框架的有趣知识。一种方法被弃用通常有一个很好的理由,理解这些原因会导致更深入的见解。
So from a learning/growing perspective, it is also a worthwhile effort
所以从学习/成长的角度来看,也是值得的
回答by James P.
Is it wrong to use Deprecated methods or classes in Java?"
在 Java 中使用已弃用的方法或类是错误的吗?”
Not wrong as such but it can save you some trouble. Here is an example where it's strongly discouraged to use a deprecated method:
没有错,但它可以为您节省一些麻烦。这是一个强烈建议不要使用已弃用方法的示例:
http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html
http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html
Why is Thread.stop deprecated?
Because it is inherently unsafe. Stopping a thread causes it to unlock all the monitors that it has locked. (The monitors are unlocked as the ThreadDeath exception propagates up the stack.) If any of the objects previously protected by these monitors were in an inconsistent state, other threads may now view these objects in an inconsistent state. Such objects are said to be damaged. When threads operate on damaged objects, arbitrary behavior can result. This behavior may be subtle and difficult to detect, or it may be pronounced. Unlike other unchecked exceptions, ThreadDeath kills threads silently; thus, the user has no warning that his program may be corrupted. The corruption can manifest itself at any time after the actual damage occurs, even hours or days in the future.
为什么不推荐使用 Thread.stop?
因为它本质上是不安全的。停止线程会使其解锁所有已锁定的监视器。(当 ThreadDeath 异常沿堆栈向上传播时,监视器被解锁。)如果以前受这些监视器保护的任何对象处于不一致状态,则其他线程现在可以查看处于不一致状态的这些对象。据说此类物品已损坏。当线程对损坏的对象进行操作时,可能会导致任意行为。这种行为可能很微妙且难以察觉,也可能很明显。与其他未经检查的异常不同,ThreadDeath 会默默地杀死线程;因此,用户没有警告他的程序可能已损坏。在实际损坏发生后的任何时间,甚至数小时或数天后,损坏都可能出现。
What if don't change any method and run my application with warnings that I have, will it create any performance issue.
如果不更改任何方法并在出现警告的情况下运行我的应用程序,是否会产生任何性能问题。
There should be no issues in terms of performance. The standard API is designed to respect some backward compatibility so applications can be gradually adapted to newer versions of Java.
性能方面应该没有问题。标准 API 旨在尊重一些向后兼容性,因此应用程序可以逐渐适应 Java 的更新版本。
回答by Chathuranga Chandrasekara
Is it wrong to use Deprecated methods or classes in Java?It is not "wrong", still working but avoid it as much as possible.
在 Java 中使用 Deprecated 方法或类是错误的吗?这不是“错误”,仍然有效,但要尽可能避免。
Suppose there is a security vulnerability associated with a method and the developers determine that it is a design flaw. So they may decide to deprecate the method and introduce the new way.
假设有一个与方法相关的安全漏洞,并且开发人员确定它是一个设计缺陷。因此,他们可能会决定弃用该方法并引入新方法。
So if you still use the old method, you have a threat. So be aware of the reason to the deprecation and check whether how it affects to you.
所以如果你仍然使用旧的方法,你就有了威胁。因此,请注意弃用的原因并检查它是否对您有影响。
what if don't change any method and run my application with warnings that I have, will it create any performance issue.
如果不更改任何方法并在出现警告的情况下运行我的应用程序,会不会产生任何性能问题。
If the deprecation is due to a performance issue, then you will suffer from a performance issue, otherwise there is no reason to have such a problem. Again would like to point out, be aware of the reason to deprecation.
如果弃用是由于性能问题,那么您将遭受性能问题,否则没有理由出现这样的问题。再次想指出的是,要注意弃用的原因。