在 Java 中使用不推荐使用的方法或类是错误的吗?

发布于 2024-09-03 09:27:10 字数 180 浏览 4 评论 0原文

我正在使用 eclipse 开发一个 Web 应用程序。就在今天,我通过更改 JAR 文件更新了我的 struts 版本。我在某些地方收到警告,指出方法已被弃用,但代码运行良好。

我想知道一些事情

  1. 在 Java 中使用已弃用的方法或类是否错误?

  2. ,会怎样?

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

  1. Is it wrong to use Deprecated methods or classes in Java?

  2. What if I don't change any method and run my application with warnings that I have, will it create any performance issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

花桑 2024-09-10 09:27:10

<强>1。在 Java 中使用不推荐使用的方法或类是错误的吗?

来自 已弃用的定义

程序员不鼓励使用带有 @Deprecated 注释的程序元素,通常是因为它很危险,或者因为存在更好的替代方案。

该方法保留在 API 中,以便向后兼容一段未指定的时间 。时间,并且可能在未来的版本中被删除。也就是说,不,这没有错误,但是有一种更好的方法可以做到这一点,这种方法对于 API 更改更加稳健。

<强>2。如果我不更改任何方法并在出现警告的情况下运行应用程序,会产生任何性能问题吗?

很可能不会。它将继续像弃用之前一样工作。 API方法的约定不会改变。如果某些内部数据结构发生变化以支持新的、更好的方法,则可能会对性能产生影响,但这不太可能。


Java API 中最有趣的弃用是我认为的 FontMetrics.getMaxDecent。弃用原因:拼写错误。

已弃用。从 JDK 版本 1.1.1 开始,由 getMaxDescent() 取代。

1. Is it wrong to use Deprecated methods or classes in Java?

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.

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.

2. What if I don't change any method and run my application with warnings that I have, will it create any performance issue.

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.

Deprecated. As of JDK version 1.1.1, replaced by getMaxDescent().

乞讨 2024-09-10 09:27:10

您仍然可以使用已弃用的代码而不改变性能,但弃用方法/类的全部目的是让用户知道现在有更好的方法来使用它,并且在将来的版本中可能会删除已弃用的代码。

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.

川水往事 2024-09-10 09:27:10

术语

来自 Sun 官方术语表:

弃用:指不再推荐的类、接口、构造函数、方法或字段,并且可能在未来版本中不再存在。

从如何以及何时弃用指南中:

您可能听说过“自嘲式幽默”这个词,或者是贬低说话者重要性的幽默。已弃用的类或方法就是这样。这已经不重要了。事实上,它是如此不重要,您不应该再使用它,因为它已被取代并且将来可能不复存在。

@Deprecated 注释更进一步并警告危险:

带有 @Deprecated 注释的程序元素是不鼓励程序员使用的,通常是因为它危险,或者因为存在更好的替代方案。

参考文献


对还是错?

使用已弃用的方法是对还是错的问题必须根据个人情况进行检查。以下是 Effective Java 第二版 中出现“已弃用” 一词的全部引用:

第 7 项:避免终结器:声称保证终结的唯一方法是 System.runFinalizersOnExit 及其邪恶孪生 Runtime.runFinalizersOnExit。这些方法存在致命缺陷,已被弃用。

第 66 项:同步对共享可变数据的访问:库提供了 Thread.stop 方法,但此方法很久以前就已被弃用,因为它本质上不安全——它的使用可能会导致数据损坏。

第 70 项:文档线程安全System.runFinalizersOnExit 方法是线程敌对的,已被弃用。

第 73 项:避免线程组:它们允许您同时将某些 Thread 原语应用到一堆线程。其中一些原语已被弃用,其余的则很少使用。 [...] 线程组已过时。

因此,至少对于上述所有方法,使用它们显然是错误的,至少根据乔什·布洛赫的说法。

使用其他方法,您必须单独考虑问题,并了解它们被弃用的原因,但一般来说,当弃用的决定是合理的时,它往往会倾向于错误而不是正确的。继续使用它们。

相关问题

Terminology

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


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:

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.

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

小情绪 2024-09-10 09:27:10

除了上面所有出色的回应之外,我发现还有另一个原因需要删除已弃用的 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

叹倦 2024-09-10 09:27:10

它当然不会造成性能问题 - 已弃用意味着将来该函数可能不再是库的一部分,因此您应该避免在新代码中使用它并更改旧代码代码停止使用它,这样当你升级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

城歌 2024-09-10 09:27:10

您可能听说过“自嘲式幽默”这个词。这种幽默会降低你的重要性。已弃用的类或方法就是这样。这已经不重要了。事实上,它是如此不重要,以至于根本不应该再使用它,因为它可能在未来不复存在。

尽量避免

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

左秋 2024-09-10 09:27:10

这没有错,只是不推荐。这通常意味着此时有更好的做事方法,如果您使用新的改进方法,您会做得很好。一些已弃用的东西确实很危险,应该完全避免。新方法可以比已弃用的方法产生更好的性能,但情况并非总是如此。

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.

和影子一齐双人舞 2024-09-10 09:27:10
  1. 一般来说不会,使用已弃用的方法并不是绝对错误的,只要您有一个良好的应急计划,以避免在这些方法从您正在使用的库中消失时出现任何问题。对于 Java API 本身来说,这种情况永远不会发生,但对于其他任何东西来说,这意味着它将被删除。如果您明确计划不升级(尽管从长远来看您很可能)软件的支持库,那么使用已弃用方法没有问题。
  2. 不。
  1. 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 using deprecated methods.
  2. No.
晨光如昨 2024-09-10 09:27:10

是的,这是错误的。

不推荐使用的方法或类将在 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.

如梦 2024-09-10 09:27:10

这并没有错,但是一些不推荐使用的方法在软件的未来版本中被删除,因此您可能最终会得到无法工作的代码。

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.

城歌 2024-09-10 09:27:10

在 Java 中使用不推荐使用的方法或类是错误的吗?”

本身并没有错,但它可以为您省去一些麻烦。下面是一个强烈建议不要使用已弃用的方法的示例

: 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

为什么 Thread.stop 被弃用?

因为它本质上是不安全的。
停止线程会导致其解锁
已锁定的所有监视器。
(监视器解锁为
ThreadDeath异常向上传播
堆栈。)如果有任何对象
以前受这些监视器的保护
处于不一致状态,其他
线程现在可以查看这些对象
不一致的状态。此类物体
据说已损坏。当线程
对损坏的物体进行任意操作
行为可能会导致。这种行为可能
微妙且难以察觉,或者
它可能是发音的。与其他不同
未检查异常、ThreadDeath
默默地杀死线程;因此,用户
没有警告他的程序可能会
已损坏。腐败现象可以表现出来
实际发生后的任何时间
损坏发生,甚至数小时或数天
未来。


如果不更改任何方法并在出现警告的情况下运行我的应用程序,会产生任何性能问题吗?

性能方面应该没有问题。标准 API 的设计考虑了向后兼容性,因此应用程序可以逐渐适应较新版本的 Java。

Is it wrong to use Deprecated methods or classes in 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

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.


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.

世俗缘 2024-09-10 09:27:10

在 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.

野却迷人 2024-09-10 09:27:10

在 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.

堇色安年 2024-09-10 09:27:10

当然不是——因为整个 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.

国产ˉ祖宗 2024-09-10 09:27:10

我觉得不推荐使用的方法意味着;有一种替代方法可用,它在各方面都比现有方法更好。使用好的方法比现有的旧方法更好。为了向后兼容,旧方法被保留为已弃用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文