CosminO :
Assuming we have a method that receives at least one parameter and potentially 0 or more parameters of the same type:
public void method(T p1, T... otherPs)
And that we have a method that calls it that looks like this:
public void callingMethod(T... params)
that we know has at least one parameter.
Is it possible to call method
from callingMethod
to look something like this?
public void callingMethod(T... params){
[...]
method(params[0], restOfParams);
[...]
}
LppEdd :
Sure, that is possible and pretty easy.
The only overhead is the creation of another array.
See Eugene answer to understand the memory consumption.
public void callingMethod(T... params){
...
method(params[0], Arrays.copyOfRange(params, 1, params.length));
...
}
A bit off topic, but I just run JOL, as suggested, and here is the output for an empty references' array
final String[] array = new String[0];
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 0x0000000800000000 base address and 0-bit shift.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
[Ljava.lang.String;@7ea37dbfd object externals:
ADDRESS SIZE TYPE PATH VALUE
71695b2d0 16 [Ljava.lang.String; []
For an array with a single, null
, element
final String array = new String[1];
[Ljava.lang.String;@7ea37dbfd object externals:
ADDRESS SIZE TYPE PATH VALUE
71692a7a8 24 [Ljava.lang.String; [null]
For an array containing a single, non-null
, element
final String array = new String[1];
array[0] = new String("");
[Ljava.lang.String;@7ea37dbfd object externals:
ADDRESS SIZE TYPE PATH VALUE
707612b68 16 [B [0].value []
707612b78 1577856 (something else) (somewhere else) (something else)
707793ef8 24 [Ljava.lang.String; [(object)]
707793f10 24 java.lang.String [0] (object)