Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What problems arise if you use deprecated methods/functions in Java?

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???

like image 350
Saher Ahwal Avatar asked Aug 08 '10 19:08

Saher Ahwal


3 Answers

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

  • 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

  • Is it wrong to use Deprecated methods or classes in Java?
  • Difference between a Deprecated and Legacy API?
like image 54
polygenelubricants Avatar answered Oct 22 '22 02:10

polygenelubricants


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

like image 3
Thorbjørn Ravn Andersen Avatar answered Oct 22 '22 00:10

Thorbjørn Ravn Andersen


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.

like image 2
BillThor Avatar answered Oct 22 '22 00:10

BillThor