Hacker News new | past | comments | ask | show | jobs | submit login

If it's for optimisation, why introduce another method call (ArrayUtil.createArray) instead of using new Object[] { .. } directly?

I can understand the backwards compatibility argument, but I just don't see the need for this ArrayUtil class.




Two things:

Calling new Object[]{} directly is going to call the constructor for Object anyway, so there's no really difference in the number of method calls for either.

Also, in terms of optimization: calling new Object[]{...} makes it so that the size of the array isn't known until runtime when the initialization parameters can be evaluated. Because array length is immutable the JVM must wait until execution to create an array of the proper size.

With the overloaded static createArray() methods used in ArrayUtils the size of the array can be determined and memory allocated (based on the number of parameters at the callsite and hence which variation of the method should be called) at the JRE's convenience/when optimal.

All of this is probably micro-optimization to the who-really-cares level, and might even be irrelevant due to javac/JIT magic, just wanted to show that the existence of the new Object[] syntax doesn't preclude there being performance gains from using something like ArrayUtil.createArray().




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: