在 Java 中使用 Deprecated 方法或类是错误的吗? [英] Is it wrong to use Deprecated methods or classes in Java?

查看:23
本文介绍了在 Java 中使用 Deprecated 方法或类是错误的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想知道一些事情

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

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

推荐答案

1.在 Java 中使用 Deprecated 方法或类是错误的吗?

来自已弃用的定义:

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

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

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.如果我不更改任何方法并在出现警告的情况下运行我的应用程序,会不会产生任何性能问题.

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

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.

Java API 中最有趣的弃用是 imo,FontMetrics.getMaxDecent.弃用原因:拼写错误.

The funniest deprecation in the Java API, is imo, the FontMetrics.getMaxDecent. Reason for deprecation: Spelling error.

已弃用.从 JDK 版本 1.1.1 开始,由 getMaxDescent() 替换.

这篇关于在 Java 中使用 Deprecated 方法或类是错误的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆