If you find that there is a definition method in a interface:
public string [] getparameters ();
Then you should reflect seriously. The array is not only old -fashioned, but we have reasonable reasons to avoid exposure. In this article, I will try to summarize the defects of array in Java API. Start with one of the most unexpected examples.
The array leads to poor performance
You may think that the use of arrays is the fastest because the array is the underlying data structure implemented by most collections. How can the use of a pure number group lower than using an array object?
Let's start with this common habit usage that looks familiar:
Public String [] getnames () {Return namesList.toarray (new string [namesList.size ()]);} This method creates a data from a variable collection where its internal preservation data is used. It tries to optimize the creation of the array by providing an array of exactly sizes. Interestingly, this "optimization" makes it compare with the following. The simpler version is slower (please see the green vs orange bar in the chart):
public string [] getnames () {Return namesList.toarray (New String [0]);} However, if the method returns a list, creating a defensive copy is faster (red bar):
Public list <string> getnames () {Return New ArrayList (namesList);} The difference is that an ArrayList put its data item in an Object [] array, and uses a type of toarray method, which is much faster than the type of method (blue bar). This is safe, which is safe. Because there is no type of array, the encapsulation is encapsulated in the generic type of the compiler.
This icon shows a reference standard for n = 5 on java 7. However, more data items or another VM situation system, this picture will not change too much. Very violent, but there will be growth. Opportunity users who have an array should be converted into a collection in order to use it to do anything, and then convert the result back to a array to send it into another interface method, so and so on. practice.
It uses a simple ArrayList instead of an array to improve performance. There is no need to move too much hands and feet. ArrayList adds 32 -bytes to the array encapsulated. For example, an array with ten objects requires 104 bytes. , ARRAYLIST 136 bytes.
Using a collection, you may even decide to return an unmodified version of the internal list:
Public list <string> getnames () {Return Collections.unmodifiableList (namesList);}This operation will run at a fixed market price, so he is much faster than any other method (yellow bar). The same defensive copy is different. A unable to modify the change will change when your internal data changes. If the change occurs, the client runs to a ConcurrentModificationException when iterated data items. It can be considered as a bad design. The interface provides a UNSUPPORTEDOPENEPTION when runtime. However, at least for internal use, this method, this method For a defensive copy, it will be a high-performance choice-some things that cannot be implemented by arrays.
The array defines a structure, not an interface
Java is an object -oriented language. The core concept of object -oriented is to provide some methods to access and operate their data instead of directly operating the data domain. Create an interface to describe what you can do on the object.
Since Java has designed performance, native types and arrays have been integrated into the type system. Objects can use arrays to store data in content efficiently. It will not provide any method to access and operate these elements. In fact, except for the replacement elements of direct access, you don't have much other things to do in the array. The array is not even meaningful to realize Tostring and Equals. But there are:
String [] array = {"foo", "bar"}; list <string> list = arrays.aslist (array); system.out.println (list); // -> [foo, bar] system.out. Println (array); //-> [ljava.lang.string;@6F548414 list.equals (arrays.aslist ("foo", "bar")) //-> trueRray.equals (new string [] {"fooo "," Bar "}) // -> FalseDifferent from the array, the collection API provides many useful methods to access the elements. Users can check the elements included, extract the sub -list or calculate the intersection. The collection can add specific features to the data layer, such as thread security, and maintain the principle of implementation at the same time. It can be seen inside.
By using a data, you define where the data is preserved in memory. By using a collection, you define the operation that users can do on the data.
The array is not a safe type
If you rely on the type of the compiler check, be careful of the object array. The following code will be defeated during runtime, but the compiler cannot find the problem:
Number [] numbers = New Integer [10]; Numbers [0] = Long.valueof (0); // Throws ArrayStoreException
The reason is that the array is "collaborative". For example, if T is a sub -type of S, then T [] will be a sub -type of S []. Joshu Block covers all theories in its works Effective Java, covering all theories, and all theories, Every Java developer must read.
Because of this behavior, the interface of the exposed array type allows a sub -type of a declaration of array type, which causes a weird runtime abnormality.
BLOCH also explained that the array is not compatible with the generic type. Because the array will be compulsory to require type information during runtime, and the generic type will be checked during compilation, the generic type cannot be placed in the array.
-Joshua Block, Effective Java (2nd Edition), Article 29
Summarize
The language structure of the bottom layer of the array and they will be used in implementation, but they should not think of other classes exposure. Using an array in one interface method to violate the object -oriented principle. Causes shortcomings to type safety and performance.