在 Java 中使用不推荐使用的方法或类是错误的吗?
我正在使用 eclipse 开发一个 Web 应用程序。就在今天,我通过更改 JAR 文件更新了我的 struts 版本。我在某些地方收到警告,指出方法已被弃用,但代码运行良好。
我想知道一些事情
在 Java 中使用已弃用的方法或类是否错误?
- ,会怎样?
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
来自 已弃用的定义:
该方法保留在 API 中,以便向后兼容一段未指定的时间 。时间,并且可能在未来的版本中被删除。也就是说,不,这没有错误,但是有一种更好的方法可以做到这一点,这种方法对于 API 更改更加稳健。
很可能不会。它将继续像弃用之前一样工作。 API方法的约定不会改变。如果某些内部数据结构发生变化以支持新的、更好的方法,则可能会对性能产生影响,但这不太可能。
Java API 中最有趣的弃用是我认为的
FontMetrics.getMaxDecent
。弃用原因:拼写错误。From the definition of 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.
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.
The funniest deprecation in the Java API, is imo, the
FontMetrics.getMaxDecent
. Reason for deprecation: Spelling error.您仍然可以使用已弃用的代码而不改变性能,但弃用方法/类的全部目的是让用户知道现在有更好的方法来使用它,并且在将来的版本中可能会删除已弃用的代码。
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.
术语
来自 Sun 官方术语表:
从如何以及何时弃用指南中:
@Deprecated
注释更进一步并警告危险:参考文献
对还是错?
使用已弃用的方法是对还是错的问题必须根据个人情况进行检查。以下是 Effective Java 第二版 中出现“已弃用” 一词的全部引用:
因此,至少对于上述所有方法,使用它们显然是错误的,至少根据乔什·布洛赫的说法。
使用其他方法,您必须单独考虑问题,并了解它们被弃用的原因,但一般来说,当弃用的决定是合理的时,它往往会倾向于错误而不是正确的。继续使用它们。
相关问题
Terminology
From the official Sun glossary:
From the how-and-when to deprecate guide:
The
@Deprecated
annotation went a step further and warn of danger: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 ALL the quotes where the word "deprecated" appears in Effective Java 2nd Edition:
So at least with all of the above methods, it's clearly wrong to use them, at least according to Josh Bloch.
With other methods, you'd have to consider the issues individually, and understand WHY they 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
除了上面所有出色的回应之外,我发现还有另一个原因需要删除已弃用的 API 调用。
在研究为什么调用被弃用时,我经常发现自己在学习有关 Java/API/框架的有趣知识。一种方法被弃用通常有充分的理由,了解这些原因可以带来更深入的见解。
所以从学习/成长的角度来看,这也是值得的努力
Aside from all the excellent responses above I found there is another reason to remove deprecated API calls.
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.
So from a learning/growing perspective, it is also a worthwhile effort
它当然不会造成性能问题 - 已弃用意味着将来该函数可能不再是库的一部分,因此您应该避免在新代码中使用它并更改旧代码代码停止使用它,这样当你升级struts并发现该功能不再存在时,你就不会遇到问题
It certainly doesn't create a performance issue -- deprecated means 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
您可能听说过“自嘲式幽默”这个词。这种幽默会降低你的重要性。已弃用的类或方法就是这样。这已经不重要了。事实上,它是如此不重要,以至于根本不应该再使用它,因为它可能在未来不复存在。
尽量避免
You may have heard the term, "self-deprecating humor". That is humor that minimizes your importance. A deprecated class or method is like that. It is no longer important. It is so unimportant, in fact, that it should no longer be used at all, as it will probably cease to exist in the future.
Try to avoid it
这没有错,只是不推荐。这通常意味着此时有更好的做事方法,如果您使用新的改进方法,您会做得很好。一些已弃用的东西确实很危险,应该完全避免。新方法可以比已弃用的方法产生更好的性能,但情况并非总是如此。
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.
已弃用的
方法并不是绝对错误的,只要您有一个良好的应急计划,以避免在这些方法从您正在使用的库中消失时出现任何问题。对于 Java API 本身来说,这种情况永远不会发生,但对于其他任何东西来说,这意味着它将被删除。如果您明确计划不升级(尽管从长远来看您很可能)软件的支持库,那么使用已弃用
方法没有问题。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.是的,这是错误的。
不推荐使用的方法或类将在 Java 的未来版本中删除,并且不应使用。在每种情况下,都应该有一个替代方案。用那个。
在某些情况下,您必须使用已弃用的类或方法才能实现项目目标。在这种情况下,你真的别无选择,只能使用它。 Java 的未来版本可能会破坏该代码,但如果这是一个要求,您就必须接受这一点。这可能不是您第一次为了满足项目要求而不得不做错事,而且肯定不会是最后一次。
当您升级到新版本的 Java 或其他库时,有时您正在使用的方法或类会被弃用。不支持已弃用的方法,但不应产生意外结果。但这并不意味着他们不会,所以请尽快切换您的代码。
弃用过程是为了确保作者有足够的时间将他们的代码从旧 API 更改为新 API。好好利用这段时间。尽快更改您的代码。
Yes, it is wrong.
Deprecated methods or classes will be removed in future versions of Java and should not be used. In each case, there should be an alternative available. Use that.
There are a couple of cases when you have to use a deprecated class or method in order to meet a project goal. In this case, you really have no choice but to use it. Future versions of Java may break that code, but if it's a requirement you have to live with that. It probably isn't the first time you had to do something wrong in order to meet a project requirement, and it certainly won't be the last.
When you upgrade to a new version of Java or some other library, sometimes a method or a class you were using becomes deprecated. Deprecated methods are not supported, but shouldn't produce unexpected results. That doesn't mean that they won't, though, so switch your code ASAP.
The deprecation process is there to make sure that authors have enough time to change their code over from an old API to a new API. Make use of this time. Change your code over ASAP.
这并没有错,但是一些不推荐使用的方法在软件的未来版本中被删除,因此您可能最终会得到无法工作的代码。
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.
本身并没有错,但它可以为您省去一些麻烦。下面是一个强烈建议不要使用已弃用的方法的示例
: sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation.html" rel="nofollow noreferrer">http://java.sun.com/j2se/1.4.2/docs/guide/misc/threadPrimitiveDeprecation .html
性能方面应该没有问题。标准 API 的设计考虑了向后兼容性,因此应用程序可以逐渐适应较新版本的 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
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.
在 Java 中使用已弃用的方法或类是否错误?
这不是“错误”,仍然有效,但尽可能避免它。
假设存在与某个方法相关的安全漏洞,并且开发人员确定它是设计缺陷。因此他们可能决定弃用该方法并引入新方法。
所以如果你仍然使用旧方法,你就有威胁。因此,请注意弃用的原因,并检查它是否对您产生影响。
如果不更改任何方法并在出现警告的情况下运行我的应用程序,是否会产生任何性能问题。
如果弃用是由于性能问题造成的,那么您将遇到性能问题,否则没有理由出现这样的问题。再次想指出,要了解弃用的原因。
Is it wrong to use Deprecated methods or classes in Java?
It is not "wrong", still working but avoid it as much as possible.
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.
在 Java 中它是 @Deprecated,在 C# 中它是 [Obsolete]。
我想我更喜欢 C# 的术语。这只是意味着它已经过时了。如果您愿意,您仍然可以使用它,但可能有更好的方法。
如果您认为 Windows 3.1 已经过时,这就像使用 Windows 3.1 而不是 Windows 7 一样。您仍然可以使用它,但未来版本中可能会有更好的功能,而且未来版本可能会受到支持 - 过时的版本不会。
对于 Java 的 @Deprecated 也是如此 - 您仍然可以使用该方法,但需要您自担风险 - 将来,它可能有更好的替代方案,甚至可能不受支持。
如果您使用的是已弃用的代码,通常没问题,只要您不必升级到较新的 API - 已弃用的代码可能不存在。我建议,如果您看到某些内容正在使用已弃用的代码,请更新以使用较新的替代方案(这通常在注释或 Javadoc 已弃用的注释中指出)。
编辑:正如迈克尔所指出的,如果弃用的原因是由于功能缺陷(或者因为该功能甚至不应该存在),那么显然,人们不应该使用已弃用的代码。
In Java it's @Deprecated, in C# it's [Obsolete].
I think I prefer C#'s terminology. It just means it's obsolete. You can still use it if you want to, but there's probably a better way.
It's like using Windows 3.1 instead of Windows 7 if you believe that Windows 3.1 is obsolete. You can still use it, but there's probably better features in a future version, plus the future versions will probably be supported - the obsolete one won't be.
Same for Java's @Deprecated - you can still use the method, but at your own risk - in future, it might have better alternatives, and might not even be supported.
If you are using code that is deprecated, it's usually fine, as long as you don't have to upgrade to a newer API - the deprecated code might not exist there. I suggest if you see something that is using deprecated code, to update to use the newer alternatives (this is usually pointed out on the annotation or in a Javadoc deprecated comment).
Edit: And as pointed out by Michael, if the reason for deprecation is due to a flaw in the functionality (or because the functionality should not even exist), then obviously, one shouldn't use the deprecated code.
当然不是——因为整个 Java 都被 @Deprecated 了:-) 只要 Java 还存在,你就可以随意使用它们。无论如何都不会注意到任何差异,除非它真的坏了。意思是——必须阅读它然后再决定。
然而,在 .Net 中,当某些东西被声明为 [Obsolete] 时,即使您以前从未使用过它,也请立即阅读它 - 您有大约 50% 的机会它比替换更有效和/或更容易使用:-))
所以总的来说,现在技术保守是非常有益的,但你必须先做你的阅读工作。
Of course not - since the whole Java is getting @Deprecated :-) you can feel free to use them for as long as Java lasts. Not going to notice any diff anyway, unless it's something really broken. Meaning - have to read about it and then decide.
In .Net however, when something is declared [Obsolete], go and read about it immediately even if you never used it before - you have about 50% chance that it's more efficient and/or easier to use than replacement :-))
So in general, it can be quite beneficial to be techno-conservative these days, but you have to do your reading chore first.
我觉得不推荐使用的方法意味着;有一种替代方法可用,它在各方面都比现有方法更好。使用好的方法比现有的旧方法更好。为了向后兼容,旧方法被保留为已弃用。
I feel that deprecated method means; there is an alternate=ive method available which is better in all aspects than existing method. Better to use the good method than existing old method. For backward compatibility, old methods are left as deprecated.