Is it wrong to use Deprecated methods or classes i

2019-01-01 08:50发布

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.

15条回答
几人难应
2楼-- · 2019-01-01 08:55

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.

查看更多
千与千寻千般痛.
3楼-- · 2019-01-01 08:57

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.

查看更多
忆尘夕之涩
4楼-- · 2019-01-01 08:58

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.

查看更多
ら面具成の殇う
5楼-- · 2019-01-01 09:01

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.

查看更多
孤独总比滥情好
6楼-- · 2019-01-01 09:04

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().

查看更多
高级女魔头
7楼-- · 2019-01-01 09:04

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

查看更多
登录 后发表回答