Anomalies in the Java platform libraries

Sometimes I look into the Java platform libraries for inspiration. There are many good design solutions, for example, this question . However, there are anomalies that cannot be emulated. Some of them are mentioned in the book "Effective the Java", Second Edition by Joshua Bloch, for example, persistent interfaces, such as java.io.ObjectStreamConstants, or behavior of the method equalsin java.net.URL. What other examples of such anomalies in the Java platform libraries do you know? Please provide as much as possible.

+3
source share
7 answers

The close () method, announcing an IOException that probably does not fire. And if you ever throw, it is very impossible to write a finally block that handles this correctly.

java.util.Enumeration and java.util.Iterator are essentially the same thing.

java.util.Vector, java.util.Hashtable and java.util.Dictionary should be deprecated.

Object.clone () and the Cloneable interface are terrible and have a lot of problems.

javax.swing.table.DefaultTableModel is evil.

java.io.Serializable defines a strange special contract with optional private methods readObject () and writeObject ().

java.util.Observable should never be used.

The finalize () method is an anomaly and should be avoided as much as possible.

API- / . java.util.Date , . java.util.Calendar get (int) set (int, int), , , . java.sql.Date java.sql.Timestamp java.util.Date : , , IS-A ( , IS-A). , java.sql.Date java.util.Date , (, , , IS-A).

+6

, .

+3

, , , , , API ( java.util.Date java.util.Calendar), . , . Joda Time .

, XML API , DOM (org.w3c.dom.Document) , , , . XSL-, (, ), . - XML-, (, JDOM, XOM, XStream ..).

XML API, , , . , org.w3c.dom.NodeList - java.util.List<Node>? NodeList getLength() item(int index), ? XML API ?

API- , , .

+3

, java.util.Hashtable java.util.Hashtable. Java (, LinkedList, HashMap ..).

- StringBuffer equals().

StringBuffer s1 = new StringBuffer("Test");
StringBuffer s2 = new StringBuffer("Test");
System.out.println(s1.equals(s2));

false. , equals() , StringBuffer, ( true, s1 s1).

+2
    int stringLength = string.length();
    int listLength = list.size();
    int arrayLength = array.length;

! Java 1.2 ...

+2
  • .
  • Java - Java.
  • API Date/Calendar, , .
  • API. , java.lang java.util, java.net java.io? , , 1.4, java.io java.nio .
  • AWT, Swing JRE. , GUI, SWT JavaFX. Java , Swing , .
  • Take a look at the JavaDoc for XMLOutputFactory.newInstance(String, ClassLoader)from the StAX API. Yes, it returns the wrong type of object. This bug is in the final JSR specification for StAX! And since StAX was included in the JRE with Java 6, you cannot just upgrade to a non-localized version. Again, if only Java stopped twisting the broken shit in the JRE ...
  • I'll stop. I could probably go all day ...
+2
source

Object.clone () and the Cloneable interface

+1
source

Source: https://habr.com/ru/post/1782508/


All Articles