The only way to find out is to parse the code. This is because varargs is a basic component of compilation time and does not change the way the program runs.
If in doubt, I would always copy the array. If you do not know that this will be a performance issue.
BTW: You can do the following.
MyCompositeObjects(MyObjects o1, MyObjects... objects) { MyCompositeObjects(MyObjects[] objects) {
However, this can do the opposite of what you want.
Another option is to use a static factory.
private MyCompositeObjects(MyObjects[] objects) { this.objects = objects; } MyCompositeObjects create(MyObjects... objects) { return new MyCompositeObjects(objects.close()); } MyCompositeObjects createNotCopied(MyObjects... objects) { return new MyCompositeObjects(objects, false); }
Use a more cumbersome method name for a less secure version. This means that if the method is chosen without a dissenting opinion, a safer version will be more likely.
Peter Lawrey
source share