4 ms·
>My guess is they probably had this before java supported such a syntax so that you could create arrays easily varargs came in Java 1.5, September 30, 2004 Gr
by lepht 16y ago
>My guess is they probably had this before java supported such a syntax so that you could create arrays easily
varargs came in Java 1.5, September 30, 2004
Groovy 1.0, January 2, 2007
Definitely could be for compiler optimizations or backwards compatibility, just providing some context for any possible historical reasons like the one you stated.
- bruceboughton 16y agoIf 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.
- lepht 16y agoTwo 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().
- GrandMasterBirt 16y agoCompatibility :) In any case, you can easily generate an ant script or whatever build groovy uses to either generate the vararg code or the insaneo source code as we see here. The point being that this should only happen when compiling groovy for java 1.4.
- lepht 16y ago>In any case, you can easily generate an ant script or whatever build groovy uses to either generate the vararg code or the insaneo source code as we see here. Yeah, I don't think anyone is implying this is handwritten. Generating a text file is trivial in any modern language/build system. >The point being that this should only happen when compiling groovy for java 1.4. This is a part of the pre-compiled Groovy runtime library, it's used regardless of what JDK version you're targeting for your Groovy application/script.