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
???
views:
219answers:
6No 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
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.
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.
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 twinRuntime.runFinalizersOnExit
) - Data corruption can occur due to inherently unsafe deprecated methods (e.g.
Thread.stop
)
References
- java.sun.com 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.
- Language guide/How and When to Deprecate APIs
- Annotation Type Deprecated API
Related questions
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 funtions 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 funtionalify has been moved to Calendar. Check the Javadoc for the to see the recommended replacement.
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.