如果您在 Java 中使用已弃用的方法/函数,会出现什么问题?

发布于 2024-09-14 03:13:30 字数 134 浏览 3 评论 0原文

是否因为使用 Java 中不推荐使用的函数而出现任何问题?如果是这样,为什么他们保留这个功能?如果你在Java中使用像这样不推荐使用的方法,这是一个坏习惯吗? java.sql.Date.getMonth 或 getYear 或 getDate ???

Do any problems arise because of using deprecated functions in Java?? If so, why do they keep this function?? Is it a bad habit if you use a deprecated method in Java like
java.sql.Date.getMonth or getYear or getDate???

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

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

发布评论

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

评论(6

誰認得朕 2024-09-21 03:13:30

一些潜在的问题是:

  • 方法可能不再存在(实践中从未出现过这种情况,但根据官方定义,不推荐使用的方法可能会从未来的 Java 中消除)
  • 由于不推荐使用的方法中存在致命缺陷,可能会出现严重的编程错误(例如 < code>System.runFinalizersOnExit 及其邪恶双胞胎 Runtime.runFinalizersOnExit
  • 由于本质上不安全的不推荐方法(例如 Thread.stop),可能会发生数据

损坏

相关问题

Some potential problems are:

  • Methods may cease to exist (this has never been the case in practice, but by the official definition, deprecated methods may be eliminated from future Java)
  • Serious programming errors may occur due to fatal flaws in deprecated methods (e.g. System.runFinalizersOnExit and its evil twin Runtime.runFinalizersOnExit)
  • Data corruption can occur due to inherently unsafe deprecated methods (e.g. Thread.stop)

References

Related questions

情愿 2024-09-21 03:13:30

实际上还没有删除任何方法,因此现有代码将继续运行。 Sun 一直非常注重向后兼容性。

主要好处是远离那些由于某种原因而工作不理想的代码,并且通常在运行时库的其他地方有替代品。因此,使用它们是一个坏习惯,只要编译器标记为弃用,您就应该小心并使用推荐的替代品。一般来说,消除编译器警告是一个好主意。

您可以在 Javadoc 中查看已弃用的内容的列表。 Java 5 列表位于 http://download。 oracle.com/javase/1.5.0/docs/api/deprecated-list.html

No methods have actually been removed yet so existing code will keep running. Sun has been very focused on backward compatability.

The primary benefit is to move away from code that for some reason has been found to work suboptimallly, and usually there is a replacement elsewhere in the runtime library. Hence it is a bad habit to use them, and you should take heed and use the recommended replacements whenever the compiler flags a deprecation. It is generally a good idea to aim to eliminate compiler warnings.

You can see a list of what is deprecated in the Javadocs. The Java 5 list is at http://download.oracle.com/javase/1.5.0/docs/api/deprecated-list.html

冷月断魂刀 2024-09-21 03:13:30

保留已弃用的方法,以便为以前版本的 Java 编写的代码仍然可以运行。如果他们只是删除了代码,那么当您将 Java 更新到新版本时,以前运行的代码将停止运行。

除了导致方法被弃用之外,使用已弃用的函数不会给您带来任何问题。但是,最好找出是什么取代了已弃用的方法。已弃用的功能将被新类中可能找到的新功能所取代。许多已弃用的日期功能已移至日历。检查 Javadoc 以了解推荐的替代方案。

Deprecated methods are kept so that code written for a previous version of Java still functions. If they just removed the code then previously functioning code would stop working when you updated Java to a new release.

Using deprecated functions will not cause you any problems beyond that which caused the method to be deprecated. However, it is best to find out what has replaced the deprecated method. The deprecated functionality will have been replaced with new functionality possibly found in a new class. Much of the deprecated Date functionality has been moved to Calendar. Check the Javadoc for the to see the recommended replacement.

迟到的我 2024-09-21 03:13:30

该代码将来可能会中断...

已弃用的函数是一个警告,表明该函数将消失。

立即寻找解决问题的替代方法,否则您将
将来有代码破解。

The code may break in the future...

deprecated functions are a warning that this function will go away.

Look for alternative ways to fix the problem now, or you will
have the code break in the future.

葬心 2024-09-21 03:13:30

已弃用的类或方法可能会在未来版本中删除。 Sun 习惯于弃用某些东西,但从不真正删除它,在我看来,这不太好,因为这使得使用已弃用的方法看起来似乎没问题。不过,您不应该尽可能使用已弃用的方法(并且应该始终是可能的)。这尤其适用于外部库,恕我直言,它们通常不像 Sun 那样害怕删除已弃用的代码。

A deprecated class or method might get removed in a future version. Sun had the habit of deprecating stuff and never actually removing it which in my opinion is not so good because it makes it seem Ok to use deprecated methods. Still you should not use deprecated methods where possible (and it should always be possible). This goes especially for external libraries which imho generally are not as afraid as Sun to remove deprecated code.

往日 2024-09-21 03:13:30

正如其他人指出的那样,Sun 从未从 JDK 中删除任何已弃用的方法,但第三方库中已弃用的方法却并非如此。这些有时确实会消失(例如,Lucene 项目在这里有一个相当“流畅”的 API,通过主要版本更新进行主要清理)。

即使在 JDK 中,被弃用的方法也意味着“现在有更好的方法可以做到这一点”,您可能应该更新代码以使用新版本。

已弃用的方法很少无法正常执行其最初的预期用途,但替代方法通常会更可靠,或者在更多情况下,或者以更通用的方式工作。这里很好的例子是您提到的日期函数(已弃用,因为它们不能很好地适应不同的区域设置/日历),或仅适用于 7 位 ASCII 的字符串到字节转换。如果没有新的接口,这些警告/限制就无法修复,或者因为有人可能依赖于“损坏的”实现,所以他们没有更新该方法,而是弃用该方法并提供替代方法。

As others have pointed out, Sun has never removed any deprecated methods from the JDK, but the same is not true with deprecated methods in third-party libraries. Those do disappear sometimes (the Lucene project for example has a rather "fluid" API here, doing major cleanups with major versionups).

Even in the JDK, the method being deprecated means that "there is a better way to do this now", and you should probably update your code to use the new version.

It is very rare that the deprecated method does not work correctly for what it was originally intended to do, but the replacement will usually work more reliable, or in more circumstances, or in a more general way. Good examples here are the Date functions that you mention (deprecated because they do not work well with different locales/calendars), or String-to-byte conversions that only work with 7-bit-ASCII. These caveats/restrictions cannot be fixed without a new interface, or because someone may depend on the "broken" implementation, so instead of updating the method, they deprecate it and provide an alternative one.

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