when using ar.toArray(new String[ar.size()])
Android studio 3.2.1 warns about pre-sized array and recommends empty array:
There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]). In older Java versions using pre-sized array was recommended, as the reflection call which is necessary to create an array of proper size was quite slow. However since late updates of OpenJDK 6 this call was intrinsified, making the performance of the empty array version the same and sometimes even better, compared to the pre-sized version. Also passing pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray call which may result in extra nulls at the end of the array, if the collection was concurrently shrunk during the operation. This inspection allows to follow the uniform style: either using an empty array (which is recommended in modern Java) or using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).
is it true for Android or just for java?
using a pre-sized array (which might be faster in older Java versions or non-HotSpot based JVMs).
because i think Android is non-HotSpot its virtual machine was Dalvik and now it is ART
The toArray() method of ArrayList is used to return an array containing all the elements in ArrayList in the correct order.
public <T> T[] toArray(T[] a)The toArray() method is used to get an array which contains all the elements in ArrayList object in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein.
Java allows creating an array of size zero. If the number of elements in a Java array is zero, the array is said to be empty. In this case you will not be able to store any element in the array; therefore the array will be empty.
Great question.
https://shipilev.net/blog/2016/arrays-wisdom-ancients/#_new_reflective_array
Bottom line:
toArray(new T[0])
seems faster, safer, and contractually cleaner, and therefore should be the default choice now. Future VM optimizations may close this performance gap fortoArray(new T[size])
, rendering the current "believed to be optimal" usages on par with an actually optimal one. Further improvements intoArray
APIs would follow the same logic astoArray(new T[0])
— the collection itself should create the appropriate storage.
It reads since late updates of OpenJDK 6
and it does not matter which run-time is being used to run it - because the language-level of the code, which runs as compiled classes on Dalvik, might be Java 6, 7, 8. it only matters which language-level the project used to compile it. For example:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Setting JavaVersion.VERSION_1_6
might possibly even disable the inspection complaint... fixing performance issues on these dated devices is probably not worth the effort - and some/most might not even be affected, because only the "earlier updates" behave different than all what followed up.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With