about Enum.values() memory allocation

Joseph D. Darcy joe.darcy at oracle.com
Thu Aug 9 23:43:47 UTC 2018


Catching up on email...

On 7/25/2018 12:48 PM, Brian Goetz wrote:
> This is essentially an API design bug; because values() returns an 
> array, and arrays are mutable, it must copy the array every time.  
> Otherwise some miscreant could change the contents of this array, and 
> other consumers of `values()` would see wrong data.
> There are a number of ways this could be improved, each with their 
> pros and cons:
>  - Add a new method, valuesAsList(), which would return an immutable 
> list which could be cached and shared.  (Arguably this is what 
> Enum::values should have done in the first place.)  Easy to add, but 
> puts the burden on users to change their code.

At the time in JDK 5.0, there was a concern about using java.util APIs 
in java.lang.* classes. Now that both java.lang and java.util packages 
are ensconced in the java.base module, such concerns are no longer germane.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20180809/c1fcbe1f/attachment.html>

More information about the compiler-dev mailing list