Is varargs array creation lazy? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Is varargs array creation lazy? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Consider the following code:

void someMethod(Object... varargParam){
    if(someCondition){
        doSomethingWith(varargParam);
    }
}

If someCondition resolves to false, varargParam is unused. Will the Object array still be created or is this creation postphoned to the first use?

Answer

varargParam is created when the call happens, also if not used in the method itself.

From JLS 15.14.2:

If the method being invoked is a variable arity method m, it necessarily has n > 0 formal parameters. The final formal parameter of m necessarily has type T[] for some T, and m is necessarily being invoked with k ≥ 0 actual argument expressions.

We are here to answer your question about Is varargs array creation lazy? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji